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/ProductDetail/SchedulesModule/AddSchedules/index.tsx
var AddSchedules_exports = {};
__export(AddSchedules_exports, {
  default: () => AddSchedules_default
});
module.exports = __toCommonJS(AddSchedules_exports);
var import_utils = require("@pisell/utils");
var import_ahooks = require("ahooks");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_customSelect = __toESM(require("../../../../../../booking/components/customSelect"));
var import_model = require("../../../../../model");
var import_serve = require("../../../../serve");
var import_useComponents = __toESM(require("../../../../../../../hooks/useComponents"));
var import_useEngineContext = __toESM(require("../../../../../../../hooks/useEngineContext"));
var import_index = require("./index.less");
var AddSchedules = ({ value, onChange, drawerContainer, isSelf }) => {
  var _a;
  const modalRef = (0, import_react.useRef)(null);
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const { appHelper } = (0, import_useEngineContext.default)();
  const createSchedules = (0, import_react.useRef)();
  const AllCreateScheduleRef = (0, import_react.useRef)(null);
  const [ScheduleCreateType, AllCreateSchedule] = (0, import_useComponents.default)({
    components: ["ScheduleCreateType", "AllCreateSchedule"]
  });
  const _getScheduleList = (0, import_ahooks.useRequest)(import_serve.getSchedules, {
    manual: true,
    onSuccess: (res) => {
    }
  });
  const onFinish = (e) => {
    const scheduleList = isSelf ? _getScheduleList == null ? void 0 : _getScheduleList.data : state.schedules.list;
    const idMap = (scheduleList == null ? void 0 : scheduleList.reduce((p, c) => {
      return { ...p, [c.id]: c };
    }, {})) || {};
    onChange(e.map((item) => idMap[item.id]));
  };
  const handleCreateSchedule = () => {
    var _a2, _b;
    (_b = (_a2 = createSchedules.current) == null ? void 0 : _a2.open) == null ? void 0 : _b.call(_a2, {
      getContainer: void 0
    });
  };
  const handleAddSchedule = (data) => {
    var _a2;
    if (isSelf) {
      _getScheduleList.mutate((oldData) => [data, ...oldData || []]);
    } else {
      dispatch({
        type: "setSchedules",
        payload: {
          ...state.schedules,
          list: [data, ...((_a2 = state == null ? void 0 : state.schedules) == null ? void 0 : _a2.list) || []]
        }
      });
    }
    modalRef.current.setValue((v) => [...v, data]);
    setTimeout(() => {
      var _a3, _b;
      (_b = (_a3 = document.getElementById(data.id)) == null ? void 0 : _a3.scrollIntoView) == null ? void 0 : _b.call(_a3, { block: "center", behavior: "smooth" });
    });
  };
  const handleEdit = (e, id) => {
    var _a2, _b;
    e.stopPropagation();
    (_b = (_a2 = AllCreateScheduleRef.current) == null ? void 0 : _a2.open) == null ? void 0 : _b.call(_a2, { id });
  };
  const handleChangeResData = (data) => {
    var _a2;
    const updateOldData = (oldData) => {
      return (oldData || []).map((item) => {
        var _a3;
        if (item.id === data.id) {
          return { ...data, name: ((_a3 = appHelper == null ? void 0 : appHelper.utils) == null ? void 0 : _a3.translationShop(data.name)) || data.name };
        }
        return item;
      });
    };
    if (isSelf) {
      _getScheduleList.mutate(updateOldData);
    } else {
      dispatch({
        type: "setSchedules",
        payload: {
          ...state.schedules,
          list: updateOldData((_a2 = state == null ? void 0 : state.schedules) == null ? void 0 : _a2.list)
        }
      });
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      ghost: true,
      type: "primary",
      className: "product-detail-card-module-add-button",
      onClick: () => {
        var _a2;
        (_a2 = modalRef.current) == null ? void 0 : _a2.open();
        if (isSelf) {
          _getScheduleList.run({ num: 999, skip: 1 });
        }
      }
    },
    import_utils.locales.getText("pisell2.text.schedules.assign-schedule")
  ), /* @__PURE__ */ import_react.default.createElement(
    import_customSelect.default,
    {
      ref: modalRef,
      rowKey: "id",
      options: isSelf ? _getScheduleList.data || [] : ((_a = state == null ? void 0 : state.schedules) == null ? void 0 : _a.list) || [],
      createItemText: import_utils.locales.getText("pisell2.text.schedules.create-new-schedule"),
      loading: _getScheduleList.loading,
      value,
      onValueChange: onFinish,
      handleCreateItem: handleCreateSchedule,
      isPreview: false,
      drawerProps: {
        title: import_utils.locales.getText("pisell2.text.schedules.assign-schedule"),
        destroyOnClose: true,
        width: 480,
        getContainer: drawerContainer || document.querySelector(
          ".pisell-lowcode__schedules-component-products"
        ) || document.body,
        rootStyle: { position: "absolute" },
        contentWrapperStyle: { boxShadow: "none" },
        titleNoWrap: true,
        ...isSelf ? { zIndex: 1e3 } : {}
      },
      mode: "multiple",
      optionRender: (item) => {
        return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__add-schedule-list-item" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__add-schedule-list-item-content" }, /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: "pisell-lowcode__add-schedule-list-item-color",
            style: { backgroundColor: item.color }
          }
        ), item.name), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__add-schedule-list-item-action-wrap" }, /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: "pisell-lowcode__add-schedule-list-item-action-edit",
            onClick: (e) => handleEdit(e, item.id)
          },
          import_utils.locales.getText("pisell2.text.edit")
        )));
      },
      filterItem: (d, search) => {
        return d.name.toLowerCase().indexOf(search.toLowerCase()) !== -1;
      }
    }
  ), ScheduleCreateType && /* @__PURE__ */ import_react.default.createElement(
    ScheduleCreateType,
    {
      ref: createSchedules,
      onChange: handleAddSchedule
    }
  ), AllCreateSchedule && /* @__PURE__ */ import_react.default.createElement(
    AllCreateSchedule,
    {
      ref: AllCreateScheduleRef,
      onChange: handleChangeResData
    }
  ));
};
var AddSchedules_default = AddSchedules;
