function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useContext, useEffect, useMemo, useRef } from "react";
import { locales } from "@pisell/utils";
import { Modal } from "antd";
import { ExclamationCircleOutlined } from "@ant-design/icons";
// @ts-ignore
import ArrowLeft from "@pisell/icon/es/ArrowLeft";
import { Context } from "../model";
import Schedules from "./Schedules";
import useSchedulesDataSource from "../hooks/useSchedulesDataSource";
import useComponents from "../../../hooks/useComponents";
import { deleteSchedule } from "./serve";
import { calcMinTimeMaxTimeBySchedules } from "./utils";
var SchedulesConnect = function SchedulesConnect() {
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var schedulesDetail = state.schedulesDetail,
    currentSelectDayDetail = state.currentSelectDayDetail,
    products = state.products,
    schedules = state.schedules;
  var _ref = schedules || {},
    scheduleAllMap = _ref.scheduleAllMap,
    scheduleMap = _ref.scheduleMap,
    list = _ref.list;
  var _ref2 = currentSelectDayDetail || {},
    date = _ref2.date,
    scheduleIds = _ref2.scheduleIds;
  var _ref3 = products || {},
    currentProduct = _ref3.currentProduct;
  var isProductSchedule = currentProduct && currentSelectDayDetail;
  var _useSchedulesDataSour = useSchedulesDataSource({
      scheduleIds: isProductSchedule ? currentProduct["schedule.ids"] : scheduleIds
    }),
    dataSource = _useSchedulesDataSour.dataSource,
    loading = _useSchedulesDataSour.loading,
    refresh = _useSchedulesDataSour.refresh;
  var currentSelectDateRef = useRef(date);
  var scheduleAllMapRef = useRef(scheduleAllMap);
  currentSelectDateRef.current = date;
  scheduleAllMapRef.current = scheduleAllMap;
  useEffect(function () {
    var _scheduleMap = calcMinTimeMaxTimeBySchedules(dataSource, scheduleAllMapRef.current, currentSelectDateRef.current);
    var newData = {
      list: dataSource,
      scheduleMap: _scheduleMap
    };
    if (!Object.keys(scheduleAllMapRef.current).length && !currentSelectDateRef.current) {
      newData = _objectSpread(_objectSpread({}, newData), {}, {
        scheduleAllMap: _scheduleMap
      });
    }
    dispatch({
      type: "setSchedules",
      payload: newData
    });
  }, [dataSource]);
  var createSchedules = useRef();
  var _useComponents = useComponents({
      components: ["ScheduleCreateType"]
    }),
    _useComponents2 = _slicedToArray(_useComponents, 1),
    ScheduleCreateType = _useComponents2[0];
  var setState = function setState(payload) {
    dispatch({
      type: "setState",
      payload: _objectSpread({}, payload)
    });
  };
  var onOpenDetail = function onOpenDetail(id) {
    if (!id) {
      var _createSchedules$curr, _createSchedules$curr2;
      return (_createSchedules$curr = createSchedules.current) === null || _createSchedules$curr === void 0 || (_createSchedules$curr2 = _createSchedules$curr.open) === null || _createSchedules$curr2 === void 0 ? void 0 : _createSchedules$curr2.call(_createSchedules$curr);
    }
    setState({
      schedulesDetail: {
        show: true,
        id: id
      }
    });
  };
  var onCloseSchedulesDetail = function onCloseSchedulesDetail(data) {
    if (data) {
      var _schedulesDetail$call;
      refresh();
      schedulesDetail === null || schedulesDetail === void 0 || (_schedulesDetail$call = schedulesDetail.callback) === null || _schedulesDetail$call === void 0 || _schedulesDetail$call.call(schedulesDetail, data);
    }
    setState({
      tabKey: (schedulesDetail === null || schedulesDetail === void 0 ? void 0 : schedulesDetail.fromTabKey) || state.tabKey,
      schedulesDetail: {
        show: false,
        id: undefined
      },
      calendarData: []
    });
    dispatch({
      type: "setCalendar",
      payload: {
        editData: [],
        isEdit: false
      }
    });
  };
  var handleCreateSchedule = function handleCreateSchedule(type) {
    setState({
      schedulesDetail: {
        show: true,
        id: undefined,
        type: type
      }
    });
  };
  var onCalendarDataChange = function onCalendarDataChange(data) {
    dispatch({
      type: "setCalendar",
      payload: {
        editData: data,
        isEdit: true
      }
    });
  };
  var onDelete = function onDelete(detail) {
    var deleteText = "";
    if ((detail === null || detail === void 0 ? void 0 : detail.relation_count) > 0 && (detail === null || detail === void 0 ? void 0 : detail.order_count) === 0) {
      // 有关联项目 无 订单
      deleteText = locales.getText("pisell2.text.schedules.delete-schedules-text2");
    } else if ((detail === null || detail === void 0 ? void 0 : detail.relation_count) > 0 && (detail === null || detail === void 0 ? void 0 : detail.order_count) > 0) {
      deleteText = locales.getText("pisell2.text.schedules.delete-schedules-text3");
    } else {
      deleteText = locales.getText("pisell2.text.schedules.delete-schedules-text1");
    }
    Modal.confirm({
      title: deleteText,
      icon: /*#__PURE__*/React.createElement(ExclamationCircleOutlined, null),
      onOk: function onOk() {
        deleteSchedule({
          id: detail.id
        }).then(function () {
          onCloseSchedulesDetail({
            id: detail.id,
            isRemove: true
          });
        });
      }
    });
  };
  var handleCloseDay = function handleCloseDay() {
    dispatch({
      type: "setState",
      payload: {
        currentSelectDayDetail: null
      }
    });
  };
  var handleGoBackProducts = function handleGoBackProducts() {
    dispatch({
      type: "setState",
      payload: {
        tabKey: "products",
        products: _objectSpread(_objectSpread({}, products), {}, {
          currentProduct: null
        })
      }
    });
  };
  var schedulesForDaySlot = useMemo(function () {
    if (!isProductSchedule) return null;
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__schedules-by-day-title"
    }, /*#__PURE__*/React.createElement(ArrowLeft, {
      className: "pisell-lowcode__schedules-by-day-go-back",
      onClick: handleGoBackProducts
    }), currentProduct.title);
  }, [isProductSchedule, currentProduct]);
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__schedules-component-schedules-wrap"
  }, /*#__PURE__*/React.createElement(Schedules, {
    loading: loading,
    scheduleMap: scheduleMap,
    dataSource: list,
    type: currentSelectDayDetail ? "forADay" : "all",
    currentSelectDayDetail: currentSelectDayDetail,
    detail: schedulesDetail,
    onClose: onCloseSchedulesDetail,
    onDelete: onDelete,
    onOpenDetail: onOpenDetail,
    onCalendarDataChange: onCalendarDataChange,
    onCloseDay: handleCloseDay,
    schedulesForDaySlot: schedulesForDaySlot
  }), ScheduleCreateType && /*#__PURE__*/React.createElement(ScheduleCreateType, {
    ref: createSchedules,
    mode: "event",
    onClickType: handleCreateSchedule
  }));
};
export default SchedulesConnect;