const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_index$1 = require("../iconfont/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./PisellImport.less");
//#region src/components/pisellImport/PisellImport.tsx
/** 默认导入图标（upload-cloud，与 Export 的 download-cloud 对应） */
const DEFAULT_ICON_TYPE = "pisell2-upload-cloud-01";
const BYTES_PER_MB = 1024 * 1024;
/**
* PisellImport 导入触发器组件
*
* @description 点击后打开文件选择，支持 accept、multiple、maxSize 校验，选择完成后回调 onImport(files)
*/
function PisellImport(props) {
	const { variant = "button", size = "medium", type = "default", disabled = false, label, icon, accept, multiple = false, maxSize, onImport, onError, className, style } = props;
	const inputRef = (0, react.useRef)(null);
	const displayLabel = label !== void 0 ? label : require_index.getText("pisell-import-button-label");
	const iconNode = icon !== void 0 ? icon : /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		type: DEFAULT_ICON_TYPE,
		className: "pisell-import-icon",
		style: {
			fontSize: 20,
			width: 20,
			height: 20,
			display: "inline-flex",
			alignItems: "center",
			justifyContent: "center"
		}
	});
	const handleClick = (0, react.useCallback)((e) => {
		var _inputRef$current;
		if (disabled) return;
		(_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.click();
	}, [disabled]);
	const handleFileChange = (0, react.useCallback)((e) => {
		const { files } = e.target;
		if (!files || files.length === 0) return;
		const fileList = Array.from(files);
		const maxBytes = maxSize != null ? maxSize * BYTES_PER_MB : void 0;
		if (maxBytes != null) {
			for (const file of fileList) if (file.size > maxBytes) {
				const rawMsg = require_index.getText("pisell-import-error-max-size");
				const message = typeof rawMsg === "string" ? rawMsg.replace(/\{\{maxSize\}\}/g, String(maxSize)) : `文件 ${file.name} 超过 ${maxSize}MB 限制`;
				onError === null || onError === void 0 || onError({
					type: "MAX_SIZE",
					message,
					file
				});
				e.target.value = "";
				return;
			}
		}
		onImport === null || onImport === void 0 || onImport(fileList, e);
		e.target.value = "";
	}, [
		maxSize,
		onImport,
		onError
	]);
	const rootClass = (0, classnames.default)("pisell-import", `pisell-import--${variant}`, `pisell-import--${size}`, `pisell-import--${type}`, disabled && "pisell-import--disabled", className);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("input", {
		ref: inputRef,
		type: "file",
		accept,
		multiple,
		onChange: handleFileChange,
		style: { display: "none" },
		tabIndex: -1,
		"aria-hidden": true
	}), /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: rootClass,
		style,
		disabled,
		onClick: handleClick,
		"aria-label": typeof displayLabel === "string" ? displayLabel : void 0
	}, /* @__PURE__ */ react.default.createElement("span", { className: "pisell-import-icon-wrap" }, iconNode), variant === "button" && /* @__PURE__ */ react.default.createElement("span", { className: "pisell-import-label" }, displayLabel)));
}
//#endregion
exports.PisellImport = PisellImport;
