import { createStore, createMemo, onCleanup } from "solid-js";
import { isServer } from "@solidjs/web";
import type {
FileUploader,
FileUploadEntry,
SendFunction,
UploadFile,
UploadProgress,
UploadStatus,
} from "./types.ts";
/**
* Primitive for uploading files with per-file and aggregate reactive state.
*
* Each file in a batch gets its own request (one `send` call per file, all in parallel).
* Per-file `progress`, `status`, `error`, and `response` are tracked in a store array
* for fine-grained reactivity — read `files[i].status` directly in JSX.
* Aggregate `progress` and `status` are derived memos across all entries.
*
* Pass a `SendFunction` to control the transport. Use the exported `fileSender` factory
* for XHR uploads, or supply your own to use fetch, WebSocket, etc. Keeping them separate
* allows bundlers to tree-shake `fileSender` when it is not used.
*
* @param send - Transport function called once per file (e.g. `fileSender("/api/upload")`)
*
* @example
* ```ts
* import { createFileUploader, fileSender } from "@solid-primitives/upload";
*
* const { upload, files, progress, status } = createFileUploader(fileSender("/api/upload"));
*
* // Per-file state in JSX:
* // {f =>