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; }
import React, { useEffect, useState } from 'react';
import classNames from 'classnames';
import dayjs from 'dayjs';
import CalendarItem from "./calendarItem";
import useEngineContext from "../../../../hooks/useEngineContext";
import "./index.less";
var EventScheduleCalendar = function EventScheduleCalendar(_ref) {
  var data = _ref.data,
    _ref$showScheduleName = _ref.showScheduleName,
    showScheduleName = _ref$showScheduleName === void 0 ? true : _ref$showScheduleName,
    _ref$showScheduleTime = _ref.showScheduleTime,
    showScheduleTime = _ref$showScheduleTime === void 0 ? true : _ref$showScheduleTime,
    className = _ref.className,
    style = _ref.style,
    onChange = _ref.onChange,
    _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'calendarView' : _ref$type;
  var context = useEngineContext();
  var utils = context.appHelper.utils || {};
  var _useState = useState(dayjs().year()),
    _useState2 = _slicedToArray(_useState, 2),
    selectedYear = _useState2[0],
    setSelectedYear = _useState2[1];
  var _useState3 = useState(new Date().toISOString().slice(0, 7)),
    _useState4 = _slicedToArray(_useState3, 2),
    currentMonth = _useState4[0],
    setCurrentMonth = _useState4[1]; // Format: YYYY-MM
  var _useState5 = useState(''),
    _useState6 = _slicedToArray(_useState5, 2),
    selectedDate = _useState6[0],
    setSelectedDate = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    selectedTimes = _useState8[0],
    setSelectedTimes = _useState8[1];

  /**
   * @title: 可用的日期数据
   * @description:
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-19 22:52
   */
  useEffect(function () {
    if (data !== null && data !== void 0 && data.length) {
      var today = new Date();
      today.setHours(0, 0, 0, 0);
      var firstAvailableDate = data.find(function (item) {
        var itemDate = new Date(item.date);
        return itemDate >= today;
      });

      // 定位到第一个可用日程对应的年、月、日和时间
      if (firstAvailableDate && firstAvailableDate !== null && firstAvailableDate !== void 0 && firstAvailableDate.date) {
        var availableDate = firstAvailableDate.date;
        var availableTimes = (firstAvailableDate === null || firstAvailableDate === void 0 ? void 0 : firstAvailableDate.times) || [];
        setSelectedYear(dayjs(availableDate).year());
        setCurrentMonth(new Date(availableDate).toISOString().slice(0, 7));
        setSelectedDate(availableDate);
        setSelectedTimes(availableTimes || []);
      }
    }
  }, [data]);

  /**
   * @title: 选中当前日期
   * @description:
   * @param {string} date
   * @param {CalendarDataItem} item
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-18 09:53
   */
  var handleSelectDate = function handleSelectDate(date, item) {
    var dateData = data === null || data === void 0 ? void 0 : data.find(function (d) {
      return d.date === date;
    });
    if (dateData) {
      setSelectedDate(date);
      setSelectedTimes(dateData.times || []);
    }
  };

  /**
   * @title: 格式化时间段
   * @description:
   * @param {any} time
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-21 16:54
   */
  var formatDateStr = function formatDateStr(time) {
    try {
      var _utils$storage, _locale$toLowerCase;
      var locale = (utils === null || utils === void 0 || (_utils$storage = utils.storage) === null || _utils$storage === void 0 ? void 0 : _utils$storage.get('umi_locale')) || 'en';
      var hour24 = dayjs(time).format('HH');
      var minutes = dayjs(time).format('mm');
      var isZh = (_locale$toLowerCase = locale.toLowerCase()) === null || _locale$toLowerCase === void 0 ? void 0 : _locale$toLowerCase.includes('zh');

      // 中文环境
      if (isZh) {
        var period = Number(hour24) < 12 ? '上午' : '下午';
        var hour12 = Number(hour24) % 12 || 12;
        return "".concat(period, " ").concat(hour12, ":").concat(minutes);
      }
      return dayjs(time).format('hh:mm a').toLowerCase();
    } catch (error) {}
  };
  useEffect(function () {
    onChange === null || onChange === void 0 || onChange(selectedYear);
  }, [selectedYear]);
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('schedule-calendar-wrap', className),
    style: style
  }, /*#__PURE__*/React.createElement("div", {
    className: "month-item-box"
  }, /*#__PURE__*/React.createElement(CalendarItem, {
    isEdit: false,
    month: currentMonth,
    schedulesData: data || [],
    selectDate: selectedDate,
    onSelect: handleSelectDate,
    onChange: function onChange(e, type) {
      setCurrentMonth(e);
      var currentYear = dayjs().year();
      var selectedYear = dayjs(e).year();
      setSelectedYear(selectedYear);
    },
    type: type
  })), type !== 'textList' ? /*#__PURE__*/React.createElement("div", {
    className: classNames('category-list')
  }, selectedTimes.map(function (d, i) {
    return /*#__PURE__*/React.createElement("div", {
      className: 'category-item',
      key: i
    }, showScheduleName ? /*#__PURE__*/React.createElement("div", {
      className: 'category-title'
    }, /*#__PURE__*/React.createElement("div", {
      className: "category-icon"
    }, /*#__PURE__*/React.createElement("span", null)), /*#__PURE__*/React.createElement("span", null, d.name)) : null, showScheduleTime ? /*#__PURE__*/React.createElement("div", {
      className: classNames('time')
    }, formatDateStr(d.start_at), " - ", formatDateStr(d.end_at)) : null);
  })) : null);
};
export default EventScheduleCalendar;