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/plus/walletEditor/components/ValidityPeriodSelector/index.tsx
var ValidityPeriodSelector_exports = {};
__export(ValidityPeriodSelector_exports, {
  default: () => ValidityPeriodSelector_default
});
module.exports = __toCommonJS(ValidityPeriodSelector_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_dayjs = __toESM(require("dayjs"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_locales = __toESM(require("../../locales"));
var import_utils = require("@pisell/utils");
var { Option } = import_antd.Select;
var DEFAULT_VALIDITY_OPTIONS = [
  { unit: "1", unit_type: "day", type: "normal" },
  { unit: "14", unit_type: "day", type: "normal" },
  { unit: "1", unit_type: "month", type: "normal" },
  { unit: "2", unit_type: "month", type: "normal" },
  { unit: "3", unit_type: "month", type: "normal" },
  { unit: "4", unit_type: "month", type: "normal" },
  { unit: "5", unit_type: "month", type: "normal" },
  { unit: "6", unit_type: "month", type: "normal" },
  { unit: "1", unit_type: "year", type: "normal" },
  { unit: "3", unit_type: "year", type: "normal" },
  { type: "forever" },
  { custom_date: "", type: "custom" }
];
var getOptionLabel = (option) => {
  if (option.type === "forever") {
    return import_utils.locales.getText("forever");
  }
  if (option.type === "custom") {
    return import_utils.locales.getText("custom");
  }
  const unitMap = {
    day: import_utils.locales.getText("day"),
    month: import_utils.locales.getText("month"),
    year: import_utils.locales.getText("year"),
    days: import_utils.locales.getText("days"),
    months: import_utils.locales.getText("months"),
    years: import_utils.locales.getText("years")
  };
  const { unit, unit_type } = option;
  if (Number(unit) > 1) {
    return `${unit} ${unitMap[unit_type + "s"]}`;
  }
  return `${unit} ${unitMap[unit_type]}`;
};
var transformValueToString = (option) => {
  if (option.type === "forever") {
    return "forever";
  }
  if (option.type === "custom") {
    return "custom";
  }
  return `${option.unit}${option.unit_type}`;
};
var transformStringToValue = (stringValue) => {
  if (stringValue === "forever") {
    return { type: "forever" };
  }
  if (stringValue === "custom") {
    return { type: "custom", custom_date: "" };
  }
  const match = stringValue.match(/^(\d+)(day|month|year)$/);
  if (match) {
    const [, unit, unit_type] = match;
    return {
      type: "normal",
      unit,
      unit_type
    };
  }
  return { type: "normal", unit: "1", unit_type: "day" };
};
var ValidityPeriodSelector = ({
  value,
  onChange,
  disabled = false,
  placeholder,
  options = DEFAULT_VALIDITY_OPTIONS
}) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(
    import_locales.default,
    ((_b = (_a = context.appHelper.utils) == null ? void 0 : _a.storage) == null ? void 0 : _b.get("umi_locale")) || "en"
  );
  const defaultPlaceholder = placeholder || import_utils.locales.getText("selectValidityPeriod");
  const [showCustomDate, setShowCustomDate] = (0, import_react.useState)(false);
  const [currentValue, setCurrentValue] = (0, import_react.useState)(value);
  const validityOptions = (0, import_react.useMemo)(() => {
    return options.map((option) => ({
      label: getOptionLabel(option),
      value: transformValueToString(option)
    }));
  }, [options]);
  (0, import_react.useEffect)(() => {
    setCurrentValue(value);
    setShowCustomDate((value == null ? void 0 : value.type) === "custom");
  }, [value]);
  const handleSelectChange = (selectedValue) => {
    const parsedValue = transformStringToValue(selectedValue);
    setCurrentValue(parsedValue);
    setShowCustomDate(parsedValue.type === "custom");
    if (parsedValue.type !== "custom") {
      onChange == null ? void 0 : onChange(parsedValue);
    }
  };
  const handleDateChange = (date, dateString) => {
    if (date && (currentValue == null ? void 0 : currentValue.type) === "custom") {
      const newValue = {
        type: "custom",
        custom_date: date.format("YYYY-MM-DD")
      };
      setCurrentValue(newValue);
      onChange == null ? void 0 : onChange(newValue);
    }
  };
  const getSelectValue = () => {
    if (!currentValue) return void 0;
    return transformValueToString(currentValue);
  };
  const renderOptions = () => {
    return validityOptions.map((option) => /* @__PURE__ */ import_react.default.createElement(Option, { key: option.label, value: option.value }, option.label));
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Select,
    {
      placeholder: defaultPlaceholder,
      onChange: handleSelectChange,
      value: getSelectValue(),
      disabled,
      style: { width: "100%" }
    },
    renderOptions()
  ), showCustomDate && /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { label: import_utils.locales.getText("custom"), style: { marginBottom: 0 } }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.DatePicker,
    {
      placeholder: import_utils.locales.getText("selectCustomDate"),
      onChange: handleDateChange,
      value: (currentValue == null ? void 0 : currentValue.type) === "custom" && currentValue.custom_date ? (0, import_dayjs.default)(currentValue.custom_date) : void 0,
      disabled,
      style: { width: "100%" }
    }
  )));
};
var ValidityPeriodSelector_default = ValidityPeriodSelector;
