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/schedules/products/ProductDetailDrawer/index.tsx
var ProductDetailDrawer_exports = {};
__export(ProductDetailDrawer_exports, {
  default: () => ProductDetailDrawer_default
});
module.exports = __toCommonJS(ProductDetailDrawer_exports);
var import_react = __toESM(require("react"));
var import_model = require("../../model");
var import_drawer = __toESM(require("../../../booking/components/drawer"));
var import_ProductDetail = __toESM(require("./ProductDetail"));
var import_index = require("./index.less");
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_serve = require("../serve");
var import_utils = require("../utils");
var import_utils2 = require("@pisell/utils");
var import_utils3 = require("../../utils");
var ProductDetailDrawer = () => {
  var _a;
  const [form] = import_antd.Form.useForm();
  const [isEdit, setIsEdit] = (0, import_react.useState)(false);
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const { products, currentSelectDayDetail } = state;
  const onClose = async (checkEdit = true) => {
    if (checkEdit && isEdit) {
      const confirmed = await (0, import_utils3.saveConfirm)();
      if (!confirmed) {
        return;
      }
    }
    setIsEdit(false);
    form.setFieldsValue({
      isAutoChange: false
    });
    setTimeout(() => {
      form.resetFields();
    }, 10);
    dispatch({
      type: "setCalendar",
      payload: {
        isEdit: false,
        editData: []
      }
    });
    dispatch({
      type: "setProducts",
      payload: {
        openProductDetailDrawer: false,
        currentProduct: null
      }
    });
  };
  const updateProductList = (param) => {
    dispatch({
      type: "setProducts",
      payload: {
        list: (0, import_utils.updateProductById)({
          products: state.products.list,
          id: products.currentProduct.id,
          schedule: param.schedules.schedule,
          scheduleMap: state.schedules.scheduleMap
        })
      }
    });
  };
  const drawerContent = (0, import_react.useMemo)(() => {
    if (!products.openProductDetailDrawer || !products.currentProduct) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement(import_ProductDetail.default, { form, currentProduct: products.currentProduct });
  }, [
    form,
    products.openProductDetailDrawer,
    products.currentProduct,
    currentSelectDayDetail
  ]);
  const _getProductDetail = (0, import_ahooks.useRequest)(import_serve.getProductDetail, {
    manual: true,
    onSuccess: ({ data }) => {
      form.setFieldsValue({
        schedule: (data == null ? void 0 : data.schedule) || [],
        max_participants: (data == null ? void 0 : data.max_participants) || [],
        isAutoChange: false
      });
    }
  });
  const _editProductDetail = (0, import_ahooks.useRequest)(import_serve.editProductDetail, {
    manual: true,
    onSuccess: ({ code }, param) => {
      updateProductList(param[0]);
      import_antd.message.success(import_utils2.locales.getText("pisell2.text.schedules.save-success"));
      setIsEdit(false);
      onClose(false);
    }
  });
  const _init = async () => {
    _getProductDetail.run({
      item_id: products.currentProduct.id,
      item_type: "product"
    });
    setIsEdit(false);
  };
  (0, import_react.useEffect)(() => {
    if (products.openProductDetailDrawer) {
      _init();
    }
  }, [products.openProductDetailDrawer]);
  const checkValues = (values) => {
    let res = {
      success: true,
      message: ""
    };
    console.log("values.max_participants", values.max_participants);
    if (values.max_participants.length) {
      let isError = values.max_participants.some((d) => {
        if (!d.max_participants_type) {
          return true;
        }
        if (d.max_participants_type === "custom-limit" && !d.max_participants_limit) {
          return true;
        }
        return false;
      });
      if (isError) {
        res.success = false;
        res.message = import_utils2.locales.getText(
          "pisell2.text.products.max-participants-error"
        );
      }
    }
    return res;
  };
  const onFinish = (values) => {
    let isValid = checkValues(values);
    if (!isValid.success) {
      return import_antd.message.warning(isValid.message);
    }
    const { isAutoChange, ...resetValues } = values;
    _editProductDetail.run({
      item_id: products.currentProduct.id,
      item_type: "product",
      schedules: resetValues
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_drawer.default,
    {
      titleNoWrap: true,
      width: 480,
      open: products.openProductDetailDrawer,
      title: ((_a = products.currentProduct) == null ? void 0 : _a.title) || "-",
      onClose: () => {
        onClose();
      },
      destroyOnClose: true,
      getContainer: () => document.querySelector(
        ".pisell-lowcode__schedules-component-products"
      ) || document.body,
      rootStyle: { position: "absolute" },
      contentWrapperStyle: { boxShadow: "none" },
      footer: /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          type: "primary",
          style: { width: "100%" },
          size: "large",
          disabled: _getProductDetail.loading || !isEdit,
          loading: _editProductDetail.loading,
          onClick: () => {
            form.submit();
          }
        },
        import_utils2.locales.getText("pisell2.text.schedules.save")
      ))
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "product-detail-drawer-body" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Skeleton, { round: true, active: true, loading: _getProductDetail.loading }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form,
      {
        form,
        onFinish,
        onValuesChange: (a, b) => {
          console.log("wzw", a, b);
          setIsEdit(true);
        }
      },
      drawerContent
    )))
  );
};
var ProductDetailDrawer_default = ProductDetailDrawer;
