import React, { useContext, useEffect, useRef } from "react";
import ScheduleList from "../../../../components/ScheduleList";
import "./index.less";
import AddSchedules from "./AddSchedules";
import { getScheduleListByProductId } from "../../../serve";
import { useMemoizedFn, useRequest } from "ahooks";
import { locales } from "@pisell/utils";
import { calcCalendarDataBySchedules } from "../../../../schedules/utils";
import { Context } from "../../../../model";
import useEngineContext from "../../../../../../hooks/useEngineContext";
import { request } from "../../../../utils";
var SchedulesModule = function SchedulesModule(_ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    drawerContainer = _ref.drawerContainer,
    _ref$ruleFn = _ref.ruleFn,
    ruleFn = _ref$ruleFn === void 0 ? function () {
      return true;
    } : _ref$ruleFn,
    tabKey = _ref.tabKey,
    previewItemChange = _ref.previewItemChange,
    _ref$isSelf = _ref.isSelf,
    isSelf = _ref$isSelf === void 0 ? false : _ref$isSelf,
    _ref$isTitle = _ref.isTitle,
    isTitle = _ref$isTitle === void 0 ? true : _ref$isTitle;
  var context = useEngineContext();
  if (isSelf) {
    request.setRequest(context.appHelper.utils.request);
  }
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var first = useRef(true);
  var _getScheduleList = useRequest(getScheduleListByProductId, {
    manual: true,
    onSuccess: function onSuccess(res) {}
  });
  var handleDeleteSchedule = function handleDeleteSchedule(item) {
    onChange === null || onChange === void 0 || onChange((value || []).filter(function (id) {
      return id !== item.id;
    }));
    _getScheduleList.mutate(function (data) {
      return data === null || data === void 0 ? void 0 : data.filter(function (d) {
        return d.id !== item.id;
      });
    });
  };
  var _onAddChange = function _onAddChange(arr) {
    onChange === null || onChange === void 0 || onChange(arr.map(function (d) {
      return d.id;
    }));
    _getScheduleList.mutate(arr);
  };
  useEffect(function () {
    // 只在首次进行,首次只有id  避免没必要的请求, 后续修改数据能拿到完整数据
    if (value && value.length && first.current === true) {
      _getScheduleList.run({
        ids: value || [],
        num: 999,
        skip: 1
      });
      first.current = false;
    }
  }, [value]);
  var setCalendarEditData = function setCalendarEditData(editData) {
    dispatch === null || dispatch === void 0 || dispatch({
      type: "setCalendar",
      payload: {
        isEdit: true,
        editData: editData
      }
    });
  };
  useEffect(function () {
    if (ruleFn !== null && ruleFn !== void 0 && ruleFn()) {
      setCalendarEditData(calcCalendarDataBySchedules(_getScheduleList.data || []));
    }
  }, [_getScheduleList.data, ruleFn === null || ruleFn === void 0 ? void 0 : ruleFn()]);
  var onClickItem = useMemoizedFn(function (item) {
    if (previewItemChange) {
      return previewItemChange(item);
    }
    dispatch === null || dispatch === void 0 || dispatch({
      type: "setState",
      payload: {
        tabKey: "schedules",
        schedulesDetail: {
          show: true,
          id: item.id,
          fromTabKey: tabKey,
          callback: function callback(newItem) {
            if ("isRemove" in newItem) {
              newItem.isRemove && handleDeleteSchedule(newItem);
            } else if (newItem) {
              _getScheduleList.mutate(function (oldData) {
                return (oldData || []).map(function (item) {
                  return item.id === newItem.id ? newItem : item;
                });
              });
            }
          }
        }
      }
    });
  });
  return /*#__PURE__*/React.createElement("div", {
    className: "product-detail-card-module"
  }, isTitle ? /*#__PURE__*/React.createElement("div", {
    className: "product-detail-card-title"
  }, locales.getText("pisell2.text.schedules.schedules-title")) : null, /*#__PURE__*/React.createElement(ScheduleList, {
    className: "product-detail-card-schedule-list",
    dataSource: _getScheduleList.data || [],
    loading: _getScheduleList.loading,
    showDeleteIcon: true,
    onDelete: handleDeleteSchedule,
    onItemClick: onClickItem
  }), /*#__PURE__*/React.createElement(AddSchedules, {
    isSelf: isSelf,
    value: _getScheduleList.data || [],
    onChange: _onAddChange,
    drawerContainer: drawerContainer
  }));
};
export default SchedulesModule;