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/index.tsx
var SchedulesModule_exports = {};
__export(SchedulesModule_exports, {
  default: () => SchedulesModule_default
});
module.exports = __toCommonJS(SchedulesModule_exports);
var import_react = __toESM(require("react"));
var import_ScheduleList = __toESM(require("../../../../components/ScheduleList"));
var import_index = require("./index.less");
var import_AddSchedules = __toESM(require("./AddSchedules"));
var import_serve = require("../../../serve");
var import_ahooks = require("ahooks");
var import_utils = require("@pisell/utils");
var import_utils2 = require("../../../../schedules/utils");
var import_model = require("../../../../model");
var import_useEngineContext = __toESM(require("../../../../../../hooks/useEngineContext"));
var import_utils3 = require("../../../../utils");
var SchedulesModule = ({
  value,
  onChange,
  drawerContainer,
  ruleFn = () => true,
  tabKey,
  previewItemChange,
  // 是否内部请求数据
  isSelf = false,
  isTitle = true
}) => {
  const context = (0, import_useEngineContext.default)();
  if (isSelf) {
    import_utils3.request.setRequest(context.appHelper.utils.request);
  }
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const first = (0, import_react.useRef)(true);
  const _getScheduleList = (0, import_ahooks.useRequest)(import_serve.getScheduleListByProductId, {
    manual: true,
    onSuccess: (res) => {
    }
  });
  const handleDeleteSchedule = (item) => {
    onChange == null ? void 0 : onChange((value || []).filter((id) => id !== item.id));
    _getScheduleList.mutate((data) => data == null ? void 0 : data.filter((d) => d.id !== item.id));
  };
  const _onAddChange = (arr) => {
    onChange == null ? void 0 : onChange(arr.map((d) => d.id));
    _getScheduleList.mutate(arr);
  };
  (0, import_react.useEffect)(() => {
    if (value && value.length && first.current === true) {
      _getScheduleList.run({ ids: value || [], num: 999, skip: 1 });
      first.current = false;
    }
  }, [value]);
  const setCalendarEditData = (editData) => {
    dispatch == null ? void 0 : dispatch({
      type: "setCalendar",
      payload: {
        isEdit: true,
        editData
      }
    });
  };
  (0, import_react.useEffect)(() => {
    if (ruleFn == null ? void 0 : ruleFn()) {
      setCalendarEditData(
        (0, import_utils2.calcCalendarDataBySchedules)(_getScheduleList.data || [])
      );
    }
  }, [_getScheduleList.data, ruleFn == null ? void 0 : ruleFn()]);
  const onClickItem = (0, import_ahooks.useMemoizedFn)((item) => {
    if (previewItemChange) {
      return previewItemChange(item);
    }
    dispatch == null ? void 0 : dispatch({
      type: "setState",
      payload: {
        tabKey: "schedules",
        schedulesDetail: {
          show: true,
          id: item.id,
          fromTabKey: tabKey,
          callback: (newItem) => {
            if ("isRemove" in newItem) {
              newItem.isRemove && handleDeleteSchedule(newItem);
            } else if (newItem) {
              _getScheduleList.mutate(
                (oldData) => (oldData || []).map(
                  (item2) => item2.id === newItem.id ? newItem : item2
                )
              );
            }
          }
        }
      }
    });
  });
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "product-detail-card-module" }, isTitle ? /* @__PURE__ */ import_react.default.createElement("div", { className: "product-detail-card-title" }, import_utils.locales.getText("pisell2.text.schedules.schedules-title")) : null, /* @__PURE__ */ import_react.default.createElement(
    import_ScheduleList.default,
    {
      className: "product-detail-card-schedule-list",
      dataSource: _getScheduleList.data || [],
      loading: _getScheduleList.loading,
      showDeleteIcon: true,
      onDelete: handleDeleteSchedule,
      onItemClick: onClickItem
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_AddSchedules.default,
    {
      isSelf,
      value: _getScheduleList.data || [],
      onChange: _onAddChange,
      drawerContainer
    }
  ));
};
var SchedulesModule_default = SchedulesModule;
