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 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, { useMemo } from 'react';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import 'dayjs/locale/en';
import { parseTimeRange, getDayjsLocale, getRelativeDay } from "./utils";
import { TimeRangeBlock, DateBlock, WeekdayBlock, DurationBlock } from "./components";
import useEngineContext from "../../hooks/useEngineContext";
import "./PisellTimeRangeDisplay.less";

/**
 * PisellTimeRangeDisplay 组件
 *
 * @description
 * 纯展示型时间区间组件，将 startAt / endAt 格式化为「时间区间 + 持续时间」。
 * - 同一天：HH:mm-HH:mm，可选显示日期/星期
 * - 跨天：起止日期时间
 * - 持续时间：d h m s 格式
 * - 字段顺序由 fieldOrder 控制，可将 duration 放在中间
 *
 * @example
 * <PisellTimeRangeDisplay
 *   startAt="2026-02-04 09:00"
 *   endAt="2026-02-04 18:00"
 * />
 * // => "09:00-18:00" + "9h"
 *
 * <PisellTimeRangeDisplay
 *   startAt={start}
 *   endAt={end}
 *   fields={{ fieldOrder: ['time', 'duration', 'date', 'weekday'] }}
 *   showDateForSameDay
 * />
 */
export var PisellTimeRangeDisplay = function PisellTimeRangeDisplay(props) {
  var _context$engine, _durationFormat$showS;
  var startAt = props.startAt,
    endAt = props.endAt,
    _props$textAlign = props.textAlign,
    textAlign = _props$textAlign === void 0 ? 'center' : _props$textAlign,
    fields = props.fields,
    _props$showDateForSam = props.showDateForSameDay,
    showDateForSameDay = _props$showDateForSam === void 0 ? true : _props$showDateForSam,
    timeFormat = props.timeFormat,
    dateFormat = props.dateFormat,
    weekdayFormat = props.weekdayFormat,
    durationFormat = props.durationFormat,
    containerStyle = props.containerStyle,
    timeStyle = props.timeStyle,
    dateStyle = props.dateStyle,
    weekdayStyle = props.weekdayStyle,
    durationStyle = props.durationStyle,
    localeProp = props.locale,
    className = props.className,
    style = props.style,
    onClick = props.onClick;
  var context = useEngineContext();
  var resolvedLocale = localeProp || (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || 'en';
  var effectiveLocale = useMemo(function () {
    return getDayjsLocale(resolvedLocale);
  }, [resolvedLocale]);
  var _useMemo = useMemo(function () {
      return parseTimeRange(startAt, endAt, effectiveLocale);
    }, [startAt, endAt, effectiveLocale]),
    start = _useMemo.start,
    end = _useMemo.end,
    isSameDay = _useMemo.isSameDay;
  var durationMs = useMemo(function () {
    return end.diff(start);
  }, [start, end]);

  // 换行顺序（右先换）：先持续时间 → 结束时间（跨天）→ 开始/结束的时间与日期 → 星期；故左到右为 星期, 日期, 时间, 持续时间
  var _ref = fields || {},
    _ref$showTimeRange = _ref.showTimeRange,
    showTimeRange = _ref$showTimeRange === void 0 ? true : _ref$showTimeRange,
    _ref$showDate = _ref.showDate,
    showDate = _ref$showDate === void 0 ? true : _ref$showDate,
    _ref$showWeekday = _ref.showWeekday,
    showWeekday = _ref$showWeekday === void 0 ? true : _ref$showWeekday,
    _ref$showDuration = _ref.showDuration,
    showDuration = _ref$showDuration === void 0 ? true : _ref$showDuration,
    _ref$fieldOrder = _ref.fieldOrder,
    fieldOrder = _ref$fieldOrder === void 0 ? ['weekday', 'date', 'time', 'duration'] : _ref$fieldOrder;
  var showSeconds = (_durationFormat$showS = durationFormat === null || durationFormat === void 0 ? void 0 : durationFormat.showSeconds) !== null && _durationFormat$showS !== void 0 ? _durationFormat$showS : false;

  // 默认不展示相对日（今天/昨天/明天），可通过 dateFormat.showRelativeDay: true 开启
  var effectiveDateFormat = useMemo(function () {
    return _objectSpread({
      showRelativeDay: false
    }, dateFormat);
  }, [dateFormat]);

  // 与 DateTimeDisplay 一致：日期与星期一体，若日期以相对日展示则不展示星期
  var referenceDate = dayjs();
  var relativeDay = effectiveDateFormat !== null && effectiveDateFormat !== void 0 && effectiveDateFormat.showRelativeDay ? getRelativeDay(start, referenceDate) : null;
  var hideWeekdayWhenDateRelative = showDate && relativeDay !== null;

  // 换行后每行对齐方式与 textAlign 一致（Figma：换行后每行居中）
  var justifyContent = textAlign === 'right' ? 'flex-end' : textAlign === 'left' ? 'flex-start' : 'center';
  var containerInlineStyle = _objectSpread({
    background: containerStyle === null || containerStyle === void 0 ? void 0 : containerStyle.background,
    borderRadius: containerStyle === null || containerStyle === void 0 ? void 0 : containerStyle.borderRadius,
    border: containerStyle === null || containerStyle === void 0 ? void 0 : containerStyle.border,
    boxShadow: containerStyle === null || containerStyle === void 0 ? void 0 : containerStyle.boxShadow,
    padding: containerStyle === null || containerStyle === void 0 ? void 0 : containerStyle.padding,
    gap: containerStyle === null || containerStyle === void 0 ? void 0 : containerStyle.gap,
    textAlign: textAlign,
    justifyContent: justifyContent
  }, style);
  var renderField = function renderField(fieldType) {
    switch (fieldType) {
      case 'time':
        return showTimeRange ? /*#__PURE__*/React.createElement(TimeRangeBlock, {
          key: "time",
          start: start,
          end: end,
          isSameDay: isSameDay,
          timeFormat: timeFormat,
          dateFormat: effectiveDateFormat,
          weekdayFormat: weekdayFormat,
          referenceDate: dayjs(),
          style: timeStyle
        }) : null;
      case 'date':
        if (!showDate) return null;
        if (!isSameDay) return null; // 跨天时时间块已含起止日期，不再单独展示
        if (!showDateForSameDay) return null;
        return /*#__PURE__*/React.createElement(DateBlock, {
          key: "date",
          date: start,
          format: effectiveDateFormat,
          referenceDate: dayjs(),
          style: dateStyle
        });
      case 'weekday':
        if (!showWeekday) return null;
        if (!isSameDay) return null; // 跨天时时间块已含起止星期，不再单独展示
        if (!showDateForSameDay) return null;
        if (hideWeekdayWhenDateRelative) return null;
        return /*#__PURE__*/React.createElement(WeekdayBlock, {
          key: "weekday",
          weekday: start,
          format: weekdayFormat,
          style: weekdayStyle
        });
      case 'duration':
        return showDuration ? /*#__PURE__*/React.createElement(DurationBlock, {
          key: "duration",
          durationMs: durationMs,
          showSeconds: showSeconds,
          locale: effectiveLocale,
          style: durationStyle
        }) : null;
      default:
        return null;
    }
  };
  var items = [];
  fieldOrder.forEach(function (fieldType) {
    var node = renderField(fieldType);
    if (node != null) {
      items.push({
        type: fieldType,
        node: node
      });
    }
  });
  if (items.length === 0) {
    return null;
  }

  // 时间/日期与持续时长之间用居中圆点 · 分隔
  var renderedWithSeparator = items.flatMap(function (_ref2, index) {
    var type = _ref2.type,
      node = _ref2.node;
    var needDotBefore = type === 'duration' && index > 0;
    var needDotAfter = type === 'duration' && index < items.length - 1;
    var sep = /*#__PURE__*/React.createElement("span", {
      className: "pisell-time-range-display-sep",
      "aria-hidden": true
    }, " \xB7 ");
    var el = /*#__PURE__*/React.cloneElement(node, {
      key: type
    });
    if (needDotBefore) return [sep, el];
    if (needDotAfter) return [el, sep];
    return [el];
  });
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-time-range-display ".concat(className || '').trim(),
    style: containerInlineStyle,
    onClick: onClick
  }, renderedWithSeparator);
};
export default /*#__PURE__*/React.memo(PisellTimeRangeDisplay);