import { t as e } from "../chunks/ui.module-BisoTevy.js";
import { i as t, l as n, r, s as i } from "../chunks/input-Bp6_Ucgr.js";
import { useCallback as a, useRef as o } from "react";
import { cn as s, handleKeyboardActivation as c } from "@ryuzaki13/react-foundation-lib/utils";
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: "Ggxyuj",
	fileName: "fthcmO",
	hiddenInput: "wyY60X"
};
//#endregion
//#region src/input-file/InputFile.tsx
function g(g) {
	let { value: _, onClear: v, onReadError: y, accept: b, allowedMime: x, maxBytes: S, label: C, description: w, placeholder: T = "Выберите файл", disabled: E, size: D, error: O, onClearError: k, className: A } = g;
	p(x), f(b);
	let j = o(null), { controlId: M, labelId: N, descriptionId: P, errorId: F, describedBy: I } = n({
		hasLabel: C != null,
		hasDescription: !!w,
		hasError: !!O
	}), L = a(async (e) => {
		let t = e.target.files?.[0];
		if (t) {
			e.target.value = "", O && k && k();
			try {
				if (g.readMode === "array-buffer") {
					let e = await m(t, {
						allowedMime: x,
						maxBytes: S,
						mode: "array-buffer"
					});
					g.onChange(e);
					return;
				}
				let e = await m(t, {
					allowedMime: x,
					maxBytes: S
				});
				g.onChange(e);
			} catch (e) {
				e instanceof d && y && y(e);
			}
		}
	}, [
		g,
		x,
		S,
		y,
		O,
		k
	]), R = a(() => {
		j.current?.click();
	}, []);
	return /* @__PURE__ */ u(i, {
		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: (b ?? x)?.join(","),
			disabled: E,
			className: h.hiddenInput,
			onChange: L,
			tabIndex: -1
		}), /* @__PURE__ */ l(t, {
			endAdornment: v ? /* @__PURE__ */ l(r, {
				onClick: v,
				disabled: E
			}) : void 0,
			endAdornmentWidth: v ? "var(--control-height)" : void 0,
			children: ({ controlClassName: t }) => /* @__PURE__ */ l("div", {
				role: "button",
				tabIndex: E ? -1 : 0,
				onClick: R,
				onKeyDown: (e) => c(e, R, { disabled: E }),
				className: s(e.uiInputControl, h.control, t, !!O && e.invalid),
				"data-disabled": E || void 0,
				"aria-invalid": !!O || void 0,
				"aria-disabled": E,
				"aria-labelledby": N,
				"aria-describedby": I,
				children: _ ? /* @__PURE__ */ l("span", {
					className: h.fileName,
					children: _.meta.name
				}) : /* @__PURE__ */ l("span", {
					className: e.uiPlaceholder,
					children: T
				})
			})
		})]
	});
}
//#endregion
export { g as InputFile };

//# sourceMappingURL=index.js.map