const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_locales = require("../../utils/locales.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_locales$1 = require("./locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
require("./index.less");
//#region src/components/taxSelect/index.tsx
const TaxSelect = (props) => {
	const { value, onChange, selectProps, tax_rate = 10, tax_title = "Tax" } = props;
	const inputNumberRef = (0, react.useRef)();
	const [form] = antd.Form.useForm();
	const [open, setOpen] = (0, react.useState)(false);
	require_useEngineContext.default();
	const useTax = antd.Form.useWatch("tax", form);
	_pisell_utils.locales.init(require_locales$1.default, require_locales.getLocale());
	(0, react.useEffect)(() => {
		document.body.id = "body";
	}, []);
	const options = (0, react.useMemo)(() => {
		return [
			{
				value: 0,
				label: (localStorage.getItem("umi_locale") === "en-US" ? tax_title : "") + _pisell_utils.locales.getText("pisell2.text.tax-free")
			},
			{
				value: tax_rate,
				label: tax_rate + "%"
			},
			{
				value: "custom",
				label: _pisell_utils.locales.getText("pisell2.text.custom")
			}
		];
	}, []);
	const _onChange = (val) => {
		if (val === "custom") {
			setOpen(true);
			setTimeout(() => {
				var _inputNumberRef$curre, _inputNumberRef$curre2;
				form.setFieldsValue({ tax: value || tax_rate });
				(_inputNumberRef$curre = (_inputNumberRef$curre2 = inputNumberRef.current).focus) === null || _inputNumberRef$curre === void 0 || _inputNumberRef$curre.call(_inputNumberRef$curre2);
			}, 10);
			return;
		}
		onChange === null || onChange === void 0 || onChange(val);
	};
	const _value = (0, react.useMemo)(() => {
		if (value === void 0 || value === null) return;
		if (options.some((d) => d.value === value)) return value;
		return `${_pisell_utils.locales.getText("pisell2.text.custom")} (${value}%)`;
	}, [value]);
	const onCancel = () => {
		form.resetFields();
		setOpen(false);
	};
	const onFinish = (values) => {
		onChange === null || onChange === void 0 || onChange(values.tax);
		onCancel();
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Select, require_objectSpread2._objectSpread2({
		options,
		size: "large",
		value: _value,
		onChange: _onChange,
		style: { width: "100%" },
		placeholder: _pisell_utils.locales.getText("pisell2.text.please-enter")
	}, selectProps)), /* @__PURE__ */ react.default.createElement(antd.Modal, {
		open,
		title: `${_pisell_utils.locales.getText("pisell2.text.custom")} ${tax_title}`,
		footer: null,
		onCancel
	}, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		onFinish
	}, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: tax_title,
		name: "tax",
		required: true
	}, /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
		ref: inputNumberRef,
		size: "large",
		min: 0,
		max: 100,
		type: "number",
		addonAfter: "%",
		style: { width: "100%" }
	}))), /* @__PURE__ */ react.default.createElement("div", { className: "tax-select-footer" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		onClick: onCancel,
		className: "tax-select-footer-item",
		size: "large"
	}, _pisell_utils.locales.getText("pisell2.text.cancel")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "primary",
		className: "tax-select-footer-item",
		size: "large",
		disabled: !useTax,
		onClick: () => {
			form.submit();
		}
	}, _pisell_utils.locales.getText("pisell2.text.confirm")))));
};
//#endregion
exports.default = TaxSelect;
