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/schedules/index.tsx
var schedules_exports = {};
__export(schedules_exports, {
  default: () => schedules_default
});
module.exports = __toCommonJS(schedules_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_ArrowLeft = __toESM(require("@pisell/icon/es/ArrowLeft"));
var import_model = require("../model");
var import_Schedules = __toESM(require("./Schedules"));
var import_useSchedulesDataSource = __toESM(require("../hooks/useSchedulesDataSource"));
var import_useComponents = __toESM(require("../../../hooks/useComponents"));
var import_serve = require("./serve");
var import_utils2 = require("./utils");
var SchedulesConnect = () => {
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const { schedulesDetail, currentSelectDayDetail, products, schedules } = state;
  const { scheduleAllMap, scheduleMap, list } = schedules || {};
  const { date, scheduleIds } = currentSelectDayDetail || {};
  const { currentProduct } = products || {};
  const isProductSchedule = currentProduct && currentSelectDayDetail;
  const { dataSource, loading, refresh } = (0, import_useSchedulesDataSource.default)({
    scheduleIds: isProductSchedule ? currentProduct["schedule.ids"] : scheduleIds
  });
  const currentSelectDateRef = (0, import_react.useRef)(date);
  const scheduleAllMapRef = (0, import_react.useRef)(scheduleAllMap);
  currentSelectDateRef.current = date;
  scheduleAllMapRef.current = scheduleAllMap;
  (0, import_react.useEffect)(() => {
    const _scheduleMap = (0, import_utils2.calcMinTimeMaxTimeBySchedules)(
      dataSource,
      scheduleAllMapRef.current,
      currentSelectDateRef.current
    );
    let newData = {
      list: dataSource,
      scheduleMap: _scheduleMap
    };
    if (!Object.keys(scheduleAllMapRef.current).length && !currentSelectDateRef.current) {
      newData = {
        ...newData,
        scheduleAllMap: _scheduleMap
      };
    }
    dispatch({
      type: "setSchedules",
      payload: newData
    });
  }, [dataSource]);
  const createSchedules = (0, import_react.useRef)();
  const [ScheduleCreateType] = (0, import_useComponents.default)({
    components: ["ScheduleCreateType"]
  });
  const setState = (payload) => {
    dispatch({
      type: "setState",
      payload: {
        ...payload
      }
    });
  };
  const onOpenDetail = (id) => {
    var _a, _b;
    if (!id) {
      return (_b = (_a = createSchedules.current) == null ? void 0 : _a.open) == null ? void 0 : _b.call(_a);
    }
    setState({
      schedulesDetail: {
        show: true,
        id
      }
    });
  };
  const onCloseSchedulesDetail = (data) => {
    var _a;
    if (data) {
      refresh();
      (_a = schedulesDetail == null ? void 0 : schedulesDetail.callback) == null ? void 0 : _a.call(schedulesDetail, data);
    }
    setState({
      tabKey: (schedulesDetail == null ? void 0 : schedulesDetail.fromTabKey) || state.tabKey,
      schedulesDetail: {
        show: false,
        id: void 0
      },
      calendarData: []
    });
    dispatch({
      type: "setCalendar",
      payload: {
        editData: [],
        isEdit: false
      }
    });
  };
  const handleCreateSchedule = (type) => {
    setState({
      schedulesDetail: {
        show: true,
        id: void 0,
        type
      }
    });
  };
  const onCalendarDataChange = (data) => {
    dispatch({
      type: "setCalendar",
      payload: {
        editData: data,
        isEdit: true
      }
    });
  };
  const onDelete = (detail) => {
    let deleteText = "";
    if ((detail == null ? void 0 : detail.relation_count) > 0 && (detail == null ? void 0 : detail.order_count) === 0) {
      deleteText = import_utils.locales.getText(
        "pisell2.text.schedules.delete-schedules-text2"
      );
    } else if ((detail == null ? void 0 : detail.relation_count) > 0 && (detail == null ? void 0 : detail.order_count) > 0) {
      deleteText = import_utils.locales.getText(
        "pisell2.text.schedules.delete-schedules-text3"
      );
    } else {
      deleteText = import_utils.locales.getText(
        "pisell2.text.schedules.delete-schedules-text1"
      );
    }
    import_antd.Modal.confirm({
      title: deleteText,
      icon: /* @__PURE__ */ import_react.default.createElement(import_icons.ExclamationCircleOutlined, null),
      onOk() {
        (0, import_serve.deleteSchedule)({ id: detail.id }).then(() => {
          onCloseSchedulesDetail({ id: detail.id, isRemove: true });
        });
      }
    });
  };
  const handleCloseDay = () => {
    dispatch({
      type: "setState",
      payload: {
        currentSelectDayDetail: null
      }
    });
  };
  const handleGoBackProducts = () => {
    dispatch({
      type: "setState",
      payload: {
        tabKey: "products",
        products: {
          ...products,
          currentProduct: null
        }
      }
    });
  };
  const schedulesForDaySlot = (0, import_react.useMemo)(() => {
    if (!isProductSchedule) return null;
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__schedules-by-day-title" }, /* @__PURE__ */ import_react.default.createElement(
      import_ArrowLeft.default,
      {
        className: "pisell-lowcode__schedules-by-day-go-back",
        onClick: handleGoBackProducts
      }
    ), currentProduct.title);
  }, [isProductSchedule, currentProduct]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__schedules-component-schedules-wrap" }, /* @__PURE__ */ import_react.default.createElement(
    import_Schedules.default,
    {
      loading,
      scheduleMap,
      dataSource: list,
      type: currentSelectDayDetail ? "forADay" : "all",
      currentSelectDayDetail,
      detail: schedulesDetail,
      onClose: onCloseSchedulesDetail,
      onDelete,
      onOpenDetail,
      onCalendarDataChange,
      onCloseDay: handleCloseDay,
      schedulesForDaySlot
    }
  ), ScheduleCreateType && /* @__PURE__ */ import_react.default.createElement(
    ScheduleCreateType,
    {
      ref: createSchedules,
      mode: "event",
      onClickType: handleCreateSchedule
    }
  ));
};
var schedules_default = SchedulesConnect;
