import { Calendar } from 'antd';
import dayjs from 'dayjs';
import React, { useMemo } from 'react';
import { Iconfont } from '@pisell/materials';
import { formatBackgroundColors, formatMonthDisplay } from "../../utils";
import useEngineContext from "../../../../hooks/useEngineContext";
import zhCN from 'antd/es/date-picker/locale/zh_CN';
import enUS from 'antd/es/date-picker/locale/en_US';
import ScheduleTimes from "./components/ScheduleTimes";
var CalendarItem = function CalendarItem(props) {
  var _utils$storage;
  var month = props.month,
    schedulesData = props.schedulesData,
    selectDate = props.selectDate,
    onSelect = props.onSelect,
    isEdit = props.isEdit,
    onChange = props.onChange,
    type = props.type;
  var context = useEngineContext();
  var utils = context.appHelper.utils || {};
  var locale = utils !== null && utils !== void 0 && (_utils$storage = utils.storage) !== null && _utils$storage !== void 0 && (_utils$storage = _utils$storage.get('umi_locale')) !== null && _utils$storage !== void 0 && _utils$storage.includes('zh') ? zhCN : enUS;
  var scheduleMap = useMemo(function () {
    return new Map(schedulesData.map(function (schedule) {
      return [schedule.date, schedule];
    }));
  }, [schedulesData]);

  /**
   * @title: 月份切换
   * @description:
   * @param {string} type
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-12 11:48
   */
  var handleMonth = function handleMonth(type) {
    var date = new Date(month + '-01');
    if (type === 'prev') {
      date.setMonth(date.getMonth() - 1);
    } else {
      date.setMonth(date.getMonth() + 1);
    }
    onChange(date.toISOString().slice(0, 7), type);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "month-item"
  }, /*#__PURE__*/React.createElement("div", {
    className: "calendar-month"
  }, /*#__PURE__*/React.createElement("div", {
    onClick: function onClick() {
      return handleMonth('prev');
    },
    className: "calendar-month-icon"
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-chevron-left"
  })), /*#__PURE__*/React.createElement("span", {
    className: "calendar-month-title"
  }, formatMonthDisplay(month)), /*#__PURE__*/React.createElement("div", {
    onClick: function onClick() {
      return handleMonth('next');
    },
    className: "calendar-month-icon"
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-chevron-right"
  }))), type === 'textList' ? /*#__PURE__*/React.createElement(ScheduleTimes, {
    month: month,
    schedulesData: schedulesData
  }) : /*#__PURE__*/React.createElement(Calendar, {
    locale: locale,
    headerRender: function headerRender() {
      return null;
    },
    fullscreen: false,
    value: dayjs(month),
    disabledDate: function disabledDate(currentDate) {
      return !schedulesData.some(function (item) {
        return currentDate.isSame(item.date);
      }) || currentDate.format('YYYY-MM') !== month;
    }
    // validRange={[
    // 	dayjs(month).startOf("month"),
    // 	dayjs(month).endOf("month"),
    // ]}
    ,
    fullCellRender: function fullCellRender(e) {
      // 是否禁用
      var isDisabled = !schedulesData.some(function (item) {
        return e.isSame(item.date);
      }) || e.format('YYYY-MM') !== month;
      var currentDayText = e.format('YYYY-MM-DD');
      var currentDayData = scheduleMap.get(currentDayText);
      // 是否当前月份
      var isCurrentMonth = e.format('YYYY-MM') === month;
      // 是否选中日期
      var isSelectDate = currentDayText === selectDate;
      var isExcluded = currentDayData === null || currentDayData === void 0 ? void 0 : currentDayData.isExcluded;
      var style = {
        // 如果是当前月份，无论是否禁用都添加红色文字颜色
        color: isCurrentMonth ? '#101828' : 'inherit'
      };
      var colors = !isEdit ? ['#7F56D9'] : (currentDayData === null || currentDayData === void 0 ? void 0 : currentDayData.color) || [];
      if (isDisabled) {
        return /*#__PURE__*/React.createElement("div", {
          style: style
        }, e.date());
      }
      style = {
        background: formatBackgroundColors(colors)
      };
      return /*#__PURE__*/React.createElement("div", {
        className: "caleddar-date-cell ".concat(isSelectDate ? 'select' : '', " ").concat(isExcluded ? 'excluded' : ''),
        onClick: function onClick() {
          return onSelect(currentDayText, scheduleMap.get(currentDayText));
        },
        style: {
          position: 'absolute',
          left: '50%',
          top: '50%',
          transform: 'translate(-50%, -50%)',
          // 使用 transform 进行居中
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center'
        }
      }, /*#__PURE__*/React.createElement("div", {
        className: "caleddar-date ".concat(colors.filter(Boolean).length ? 'has-status' : '', " ").concat(isCurrentMonth ? '' : 'disabled-date'),
        style: style
      }, e.date()));
    }
  }));
};
export default CalendarItem;