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/ecocup/components/SettingModal/index.tsx
var SettingModal_exports = {};
__export(SettingModal_exports, {
  default: () => SettingModal_default
});
module.exports = __toCommonJS(SettingModal_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_index = require("./index.less");
var import_hooks = require("../../hooks");
var import_const = require("./const");
var import_compensatedPisellContainer = __toESM(require("../../../../pro/compensatedPisellContainer"));
var import_responsive = require("../../../../responsive");
var SettingModal = (props) => {
  const { visible, onCancel, variant } = props;
  const currentVariant = variant || "desktop";
  const isPhoneVariant = currentVariant === "phone";
  const [form] = import_antd.Form.useForm();
  const { post, get } = (0, import_hooks.useRequest)();
  const [loading, setLoading] = (0, import_react.useState)(false);
  const _onCancel = () => {
    if (loading) return;
    onCancel == null ? void 0 : onCancel();
    form.resetFields();
  };
  const _onOk = async () => {
    if (loading) return;
    try {
      const values = await form.validateFields();
      const result = await postSetting(values);
      if (!result) {
        return;
      }
      _onCancel();
    } catch (error) {
      console.error(error);
    }
  };
  const getSetting = async () => {
    const params = {
      keys: ["eco_cup_customer_rent_config", "eco_cup_rent_expire_config"]
    };
    const res = await get("/shop/metadata/many", params);
    if ((res == null ? void 0 : res.code) == 200) {
      const { data } = res;
      const expireConfig = (data == null ? void 0 : data.eco_cup_rent_expire_config) || {};
      const rentConfig = (data == null ? void 0 : data.eco_cup_customer_rent_config) || [];
      const formValues = {
        expire: (expireConfig == null ? void 0 : expireConfig.expire) || 30
      };
      rentConfig.forEach((item) => {
        if (item == null ? void 0 : item.name) {
          formValues[item.name] = Number(item == null ? void 0 : item.max_rent_count) || void 0;
        }
      });
      form.setFieldsValue(formValues);
    }
  };
  (0, import_react.useEffect)(() => {
    getSetting();
  }, []);
  const postSetting = async (values) => {
    const { expire, ...membershipValues } = values;
    const eco_cup_customer_rent_config = import_const.formItemsConfig.map((config) => ({
      name: config.name,
      max_rent_count: membershipValues[config.name]
    }));
    const params = {
      eco_cup_customer_rent_config,
      eco_cup_rent_expire_config: {
        expire,
        unit: "day"
      }
    };
    try {
      setLoading(true);
      const res = await post("/shop/metadata/many", params);
      if ((res == null ? void 0 : res.code) == 200) {
        (0, import_materials.PisellToast)({
          content: import_utils.locales.getText("pisell2.text.ecocup.setting.success")
        });
        return true;
      }
      return false;
    } catch (error) {
      console.error("提交设置失败:", error);
      return false;
    } finally {
      setLoading(false);
    }
  };
  const renderFooter = () => /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-setting-modal-footer" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { size: "large", onClick: _onCancel, disabled: loading }, import_utils.locales.getText("pisell2.text.ecocup.button.cancel")), /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { size: "large", type: "primary", onClick: _onOk, loading }, import_utils.locales.getText("pisell2.text.ecocup.button.confirm")));
  const title = import_utils.locales.getText(
    "pisell2.text.cup-project.footer.button.setting"
  );
  const containerClassName = (0, import_classnames.default)(
    "pisell2-ecocup-setting-modal",
    `pisell2-ecocup-setting-modal--${currentVariant}`
  );
  return /* @__PURE__ */ import_react.default.createElement(
    import_compensatedPisellContainer.default,
    {
      renderType: isPhoneVariant ? "drawer" : "modal",
      showHandlerBar: isPhoneVariant,
      onHandlerBarClick: _onCancel,
      props: isPhoneVariant ? {
        open: visible,
        placement: "bottom",
        width: "100%",
        height: "auto",
        style: {
          maxHeight: "95dvh"
        },
        title,
        onClose: _onCancel,
        destroyOnClose: true,
        footer: renderFooter(),
        rootClassName: containerClassName,
        className: containerClassName,
        bodyStyle: {
          padding: "0 24px"
        }
      } : {
        open: visible,
        title,
        onCancel: _onCancel,
        width: 680,
        centered: true,
        destroyOnClose: true,
        footer: renderFooter(),
        className: containerClassName,
        wrapClassName: containerClassName,
        bodyStyle: {
          padding: "0 24px",
          maxHeight: "min(560px, calc(100dvh - 220px))",
          overflowY: "auto"
        }
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-setting-modal-content" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, size: "large" }, import_const.formItemsConfig.map((config) => /* @__PURE__ */ import_react.default.createElement(
      import_materials.FormItemInputNumber,
      {
        key: config.name,
        renderMode: "edit",
        name: config.name,
        label: import_utils.locales.getText(config.labelKey),
        placeholder: import_utils.locales.getText(config.placeholderKey),
        maxobj: {
          enabled: true,
          max: 9999,
          message: import_utils.locales.getText(
            "pisell2.text.ecocup.setting.max-rent-count-max"
          )
        },
        minobj: {
          enabled: true,
          min: 0,
          message: import_utils.locales.getText(
            "pisell2.text.ecocup.setting.max-rent-count-min"
          )
        },
        requiredobj: {
          required: true,
          message: import_utils.locales.getText(config.requiredMessageKey)
        }
      }
    )), /* @__PURE__ */ import_react.default.createElement(
      import_materials.FormItemInputNumber,
      {
        renderMode: "edit",
        name: "expire",
        label: import_utils.locales.getText("pisell2.text.ecocup.setting.expire-time"),
        placeholder: import_utils.locales.getText(
          "pisell2.text.ecocup.setting.expire-time-placeholder"
        ),
        maxobj: {
          enabled: true,
          max: 365,
          message: import_utils.locales.getText(
            "pisell2.text.ecocup.setting.expire-time-max"
          )
        },
        minobj: {
          enabled: true,
          min: 0,
          message: import_utils.locales.getText(
            "pisell2.text.ecocup.setting.expire-time-min"
          )
        },
        suffix: import_utils.locales.getText("pisell2.text.ecocup.setting.days"),
        requiredobj: {
          required: true,
          message: import_utils.locales.getText(
            "pisell2.text.ecocup.setting.expire-time-required"
          )
        }
      }
    )))
  );
};
SettingModal.displayName = "SettingModal";
var SettingModal_default = (0, import_responsive.withResponsive)(SettingModal);
