import { t as e } from "./ui.module-DHVTOqZ0.js";
import { c as t, d as n, i as r } from "./input-CCdBtl-C.js";
import { useCallback as i, useRef as a } from "react";
import { cn as o, handleKeyboardActivation as s } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as c, jsxs as l } from "react/jsx-runtime";
import { XIcon as u } from "lucide-react";
import { ReadFileError as d, assertValidAccept as f, assertValidAllowedMime as p, readFile as m } from "@ryuzaki13/react-foundation-lib/file";
var h = {
	control: "h9SMea",
	hiddenInput: "rUyh0y",
	fileList: "Q0CY1r",
	fileItem: "AJuYXd",
	fileItemName: "CKKkOD",
	removeButton: "UgxhS1"
};
//#endregion
//#region src/input-files/InputFilesList.tsx
function g({ files: e, onChange: t }) {
	let n = i((n) => {
		t(e.filter((e, t) => t !== n));
	}, [e, t]);
	return !e || !e.length ? null : /* @__PURE__ */ c("ul", {
		className: h.fileList,
		children: e.map((e, t) => /* @__PURE__ */ l("li", {
			className: h.fileItem,
			children: [/* @__PURE__ */ c("span", {
				className: h.fileItemName,
				children: e.meta.name
			}), /* @__PURE__ */ c("button", {
				type: "button",
				className: o(h.removeButton, "flexCenter"),
				onClick: () => n(t),
				"aria-label": `Удалить ${e.meta.name}`,
				children: /* @__PURE__ */ c(u, {})
			})]
		}, `${e.meta.name}-${e.meta.lastModified}`))
	});
}
//#endregion
//#region src/input-files/InputFiles.tsx
function _(u) {
	let { value: _, onReadError: v, accept: y, allowedMime: b, maxBytes: x, maxFiles: S, label: C, description: w, placeholder: T = "Выберите файлы", disabled: E, size: D, error: O, onClearError: k, className: A } = u;
	p(b), f(y);
	let j = a(null), { controlId: M, labelId: N, descriptionId: P, errorId: F, describedBy: I } = n({
		hasLabel: C != null,
		hasDescription: !!w,
		hasError: !!O
	}), L = typeof S == "number" && _.length >= S, R = i(async (e) => {
		let t = e.target.files;
		if (!t || t.length === 0) return;
		let n = Array.from(t);
		e.target.value = "", O && k && k();
		let r = typeof S == "number" ? S - _.length : n.length, i = n.slice(0, r);
		if (u.readMode === "array-buffer") {
			let e = [];
			for (let t of i) try {
				let n = await m(t, {
					allowedMime: b,
					maxBytes: x,
					mode: "array-buffer"
				});
				e.push(n);
			} catch (e) {
				e instanceof d && v && v(e);
			}
			e.length > 0 && u.onChange([...u.value, ...e]);
			return;
		}
		let a = [];
		for (let e of i) try {
			let t = await m(e, {
				allowedMime: b,
				maxBytes: x
			});
			a.push(t);
		} catch (e) {
			e instanceof d && v && v(e);
		}
		a.length > 0 && u.onChange([...u.value, ...a]);
	}, [
		u,
		S,
		_.length,
		b,
		x,
		v,
		O,
		k
	]), z = i(() => {
		j.current?.click();
	}, []), B = (u.readMode, /* @__PURE__ */ c(g, {
		files: u.value,
		onChange: u.onChange
	}));
	return /* @__PURE__ */ l(t, {
		label: C,
		description: w,
		disabled: E,
		className: A,
		size: D,
		error: O,
		controlId: M,
		labelId: N,
		descriptionId: P,
		errorId: F,
		children: [
			/* @__PURE__ */ c("input", {
				ref: j,
				id: M,
				type: "file",
				accept: (y ?? b)?.join(","),
				multiple: !0,
				disabled: E || L,
				className: h.hiddenInput,
				onChange: R,
				tabIndex: -1
			}),
			/* @__PURE__ */ c(r, { children: () => /* @__PURE__ */ c("div", {
				role: "button",
				tabIndex: E || L ? -1 : 0,
				onClick: z,
				onKeyDown: (e) => s(e, z, { disabled: E || L }),
				className: o(e.uiInputControl, h.control, !!O && "invalid"),
				"data-disabled": E || void 0,
				"aria-invalid": !!O || void 0,
				"aria-disabled": E || L,
				"aria-labelledby": N,
				"aria-describedby": I,
				children: /* @__PURE__ */ c("span", {
					className: e.uiPlaceholder,
					children: L ? `Максимум ${S} файлов` : T
				})
			}) }),
			B
		]
	});
}
//#endregion
export { g as n, _ as t };

//# sourceMappingURL=input-files-TXdXrSAs.js.map