import dayjs from 'dayjs';
import useEngineContext from "../../hooks/useEngineContext";

/**
 * @title: 获取背景色
 * @description:
 * @param {string} colors
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-01-11 22:44
 */
export var formatBackgroundColors = function formatBackgroundColors(colors) {
  var _colors = Array.from(new Set(colors.map(function (color) {
    return color === null || color === void 0 ? void 0 : color.toLocaleLowerCase();
  }) || [])).filter(function (d) {
    return !!d;
  });
  var colorStr = _colors.map(function (color, index) {
    return "".concat(color, " ").concat(index * 100 / _colors.length, "% ").concat((index + 1) * 100 / _colors.length, "%");
  }).join(', ');
  if (colorStr) {
    return "conic-gradient(".concat(colorStr, ")");
  }
  return '';
};

/**
 * @title: 获取可用的 schedule
 * @description:
 * @param {any} schedule
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-03-05 18:05
 */
export var getUsableSchedules = function getUsableSchedules(schedule) {
  if (!schedule) {
    return true;
  }
  if (!schedule.minTime || !schedule.maxTime) {
    return false;
  }
  var currentDate = dayjs();
  // 如果最大时间在当前时间之前, 代表没有可用的日程
  if (currentDate.isAfter(schedule.maxTime)) {
    return false;
  }
  return true;
};

/**
 * @title: 获取 schedules 的所有颜色
 * @description:
 * @param {any} schedules
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-03-05 18:02
 */
export var getScheduleColors = function getScheduleColors(schedules) {
  return formatBackgroundColors(schedules.map(function (d) {
    return d.color;
  }));
};

/**
 * @title: 格式化日期头部日期显示
 * @description:
 * @param {string} dateString
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-01-12 11:55
 */
export var formatMonthDisplay = function formatMonthDisplay(dateString) {
  var _utils$storage;
  var context = useEngineContext();
  var utils = context.appHelper.utils || {};
  var locale = (utils === null || utils === void 0 || (_utils$storage = utils.storage) === null || _utils$storage === void 0 ? void 0 : _utils$storage.get('umi_locale')) || 'en-US';
  var date = new Date(dateString + '-01');
  return date.toLocaleString(locale, {
    month: 'long',
    year: 'numeric'
  });
};

/**
 * @title: 资源名称
 * @description:
 * @param {any} data
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-02-11 13:39
 */
export var resourceTitle = function resourceTitle(data) {
  var _data$initData, _form$setting, _context$appHelper;
  var context = useEngineContext();
  var form = data === null || data === void 0 || (_data$initData = data.initData) === null || _data$initData === void 0 || (_data$initData = _data$initData.boardSetting) === null || _data$initData === void 0 || (_data$initData = _data$initData.event_v2_form) === null || _data$initData === void 0 ? void 0 : _data$initData.form;
  var title = (form === null || form === void 0 || (_form$setting = form.setting) === null || _form$setting === void 0 ? void 0 : _form$setting.title) || (form === null || form === void 0 ? void 0 : form.title);
  var translation = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.translation;
  return translation(title);
};