var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/taxSelect/index.tsx
var taxSelect_exports = {};
__export(taxSelect_exports, {
  default: () => taxSelect_default
});
module.exports = __toCommonJS(taxSelect_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_utils = require("@pisell/utils");
var import_locales = __toESM(require("./locales"));
var import_index = require("./index.less");
var import_locales2 = require("../../utils/locales");
var TaxSelect = (props) => {
  const {
    value,
    onChange,
    selectProps,
    tax_rate = 10,
    tax_title = "Tax"
  } = props;
  const inputNumberRef = (0, import_react.useRef)();
  const [form] = import_antd.Form.useForm();
  const [open, setOpen] = (0, import_react.useState)(false);
  const context = (0, import_useEngineContext.default)();
  const useTax = import_antd.Form.useWatch("tax", form);
  import_utils.locales.init(import_locales.default, (0, import_locales2.getLocale)());
  (0, import_react.useEffect)(() => {
    document.body.id = "body";
  }, []);
  const options = (0, import_react.useMemo)(() => {
    const isEn = localStorage.getItem("umi_locale") === "en-US";
    const taxOptions = [
      {
        value: 0,
        label: (isEn ? tax_title : "") + import_utils.locales.getText("pisell2.text.tax-free")
      },
      {
        value: tax_rate,
        label: tax_rate + "%"
      },
      {
        value: "custom",
        label: import_utils.locales.getText("pisell2.text.custom")
      }
    ];
    return taxOptions;
  }, []);
  const _onChange = (val) => {
    if (val === "custom") {
      setOpen(true);
      setTimeout(() => {
        var _a, _b;
        form.setFieldsValue({
          tax: value || tax_rate
        });
        (_b = (_a = inputNumberRef.current).focus) == null ? void 0 : _b.call(_a);
      }, 10);
      return;
    }
    onChange == null ? void 0 : onChange(val);
  };
  const _value = (0, import_react.useMemo)(() => {
    if (value === void 0 || value === null) {
      return;
    }
    if (options.some((d) => d.value === value)) {
      return value;
    }
    return `${import_utils.locales.getText("pisell2.text.custom")} (${value}%)`;
  }, [value]);
  const onCancel = () => {
    form.resetFields();
    setOpen(false);
  };
  const onFinish = (values) => {
    onChange == null ? void 0 : onChange(values.tax);
    onCancel();
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Select,
    {
      options,
      size: "large",
      value: _value,
      onChange: _onChange,
      style: { width: "100%" },
      placeholder: import_utils.locales.getText("pisell2.text.please-enter"),
      ...selectProps
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Modal,
    {
      open,
      title: `${import_utils.locales.getText("pisell2.text.custom")} ${tax_title}`,
      footer: null,
      onCancel
    },
    /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, layout: "vertical", onFinish }, /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { label: tax_title, name: "tax", required: true }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.InputNumber,
      {
        ref: inputNumberRef,
        size: "large",
        min: 0,
        max: 100,
        type: "number",
        addonAfter: "%",
        style: { width: "100%" }
      }
    ))),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "tax-select-footer" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        onClick: onCancel,
        className: "tax-select-footer-item",
        size: "large"
      },
      import_utils.locales.getText("pisell2.text.cancel")
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        type: "primary",
        className: "tax-select-footer-item",
        size: "large",
        disabled: !useTax,
        onClick: () => {
          form.submit();
        }
      },
      import_utils.locales.getText("pisell2.text.confirm")
    ))
  ));
};
var taxSelect_default = TaxSelect;
