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