import { t as e } from "./ui.module-BisoTevy.js";
import { i as t, l as n, s as r } from "./input-BCJfUdK_.js";
import { useCallback as i, useRef as a } from "react";
import { cn as o, handleKeyboardActivation as s } from "@ryuzaki13/react-foundation-lib/utils";
import { XIcon as c } from "lucide-react";
import { jsx as l, jsxs as u } from "react/jsx-runtime";
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__ */ l("ul", {
		className: h.fileList,
		children: e.map((e, t) => /* @__PURE__ */ u("li", {
			className: h.fileItem,
			children: [/* @__PURE__ */ l("span", {
				className: h.fileItemName,
				children: e.meta.name
			}), /* @__PURE__ */ l("button", {
				type: "button",
				className: o(h.removeButton, "flexCenter"),
				onClick: () => n(t),
				"aria-label": `Удалить ${e.meta.name}`,
				children: /* @__PURE__ */ l(c, {})
			})]
		}, `${e.meta.name}-${e.meta.lastModified}`))
	});
}
//#endregion
//#region src/input-files/InputFiles.tsx
function _(c) {
	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 } = c;
	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 (c.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 && c.onChange([...c.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 && c.onChange([...c.value, ...a]);
	}, [
		c,
		S,
		_.length,
		b,
		x,
		v,
		O,
		k
	]), z = i(() => {
		j.current?.click();
	}, []), B = (c.readMode, /* @__PURE__ */ l(g, {
		files: c.value,
		onChange: c.onChange
	}));
	return /* @__PURE__ */ u(r, {
		label: C,
		description: w,
		disabled: E,
		className: A,
		size: D,
		error: O,
		controlId: M,
		labelId: N,
		descriptionId: P,
		errorId: F,
		children: [
			/* @__PURE__ */ l("input", {
				ref: j,
				id: M,
				type: "file",
				accept: (y ?? b)?.join(","),
				multiple: !0,
				disabled: E || L,
				className: h.hiddenInput,
				onChange: R,
				tabIndex: -1
			}),
			/* @__PURE__ */ l(t, { children: () => /* @__PURE__ */ l("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__ */ l("span", {
					className: e.uiPlaceholder,
					children: L ? `Максимум ${S} файлов` : T
				})
			}) }),
			B
		]
	});
}
//#endregion
export { g as n, _ as t };

//# sourceMappingURL=input-files-Dcc5K-_1.js.map