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, useMemo, useState } from 'react';
import { getText } from "../../../../locales";
import { NoteTicker } from "./NoteTicker";
import { RESOURCE_WALL_NOTE_MARQUEE_MIN_CHARS } from "./resourceWallConstants";
import { buildVenueWallCardPresentation } from "./venueWallCardPresentation";
import { getVenueWallStatusToneMap, alphaColor } from "./venueWallUtils";
import "./VenueWallCard.less";

/** 暂时隐藏左上角资源类型（form_code）展示；恢复时改为 true 即可 */
var SHOW_RESOURCE_TYPE_LABEL = false;

/**
 * 将服务端给出的「整分钟」计数与收到时的墙钟对齐，随 `now` 递减；
 * `value` 或 `resourceId` 从上游变更时重新锚定。
 */
function useDriftMinutesFromEpoch(value, resourceId, now) {
  var allowNegative = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  var _useState = useState(function () {
      return {
        min: value !== null && value !== void 0 ? value : 0,
        at: Date.now()
      };
    }),
    _useState2 = _slicedToArray(_useState, 2),
    epoch = _useState2[0],
    setEpoch = _useState2[1];
  useEffect(function () {
    setEpoch({
      min: value !== null && value !== void 0 ? value : 0,
      at: Date.now()
    });
  }, [resourceId, value]);
  var elapsedMs = now - epoch.at;
  var precise = epoch.min - elapsedMs / 60000;
  var drifted = epoch.min - Math.floor(elapsedMs / 60000);
  var whole = allowNegative ? drifted : Math.max(0, drifted);
  var preciseClamped = allowNegative ? precise : Math.max(0, precise);
  return {
    whole: whole,
    precise: preciseClamped
  };
}

/**
 * 迟到分钟等：自服务给出的整分起，随 `now` 递增；`value` 或 `resourceId` 变化时重锚定。
 */
function useDriftAccumulatedMinutesFromEpoch(value, resourceId, now) {
  var _useState3 = useState(function () {
      return {
        min: value !== null && value !== void 0 ? value : 0,
        at: Date.now()
      };
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    epoch = _useState4[0],
    setEpoch = _useState4[1];
  useEffect(function () {
    setEpoch({
      min: value !== null && value !== void 0 ? value : 0,
      at: Date.now()
    });
  }, [resourceId, value]);
  return epoch.min + Math.floor((now - epoch.at) / 60000);
}

/** 下一预约行里「无数据」占位（不展示联系人兜底） */
var NEXT_LIST_PLACEHOLDER_VALUES = new Set(['', '-', '–', '—', '―', "\u2014", "\u2013"]);
function nextListFieldIsMeaningful(raw) {
  var s = String(raw !== null && raw !== void 0 ? raw : '').trim();
  if (!s) return false;
  return !NEXT_LIST_PLACEHOLDER_VALUES.has(s);
}
var DEFAULT_OCCUPIED = '#6f4ae6';
/** 空闲态卡片主色（淡灰，随 alpha 打底条等） */
var DEFAULT_AVAILABLE = '#94a3b8';
function VenueWallCardInner(_ref) {
  var _card$nextList, _card$resource, _card$area;
  var card = _ref.card,
    wallNowMs = _ref.wallNowMs,
    _ref$boardingSoonBefo = _ref.boardingSoonBeforeMin,
    boardingSoonBeforeMin = _ref$boardingSoonBefo === void 0 ? 10 : _ref$boardingSoonBefo,
    _ref$occupiedCardColo = _ref.occupiedCardColor,
    occupiedCardColor = _ref$occupiedCardColo === void 0 ? DEFAULT_OCCUPIED : _ref$occupiedCardColo,
    _ref$availableCardCol = _ref.availableCardColor,
    availableCardColor = _ref$availableCardCol === void 0 ? DEFAULT_AVAILABLE : _ref$availableCardCol,
    statusToneMapProp = _ref.statusToneMap,
    _ref$appearance = _ref.appearance,
    appearance = _ref$appearance === void 0 ? 'light' : _ref$appearance;
  var toneMap = statusToneMapProp !== null && statusToneMapProp !== void 0 ? statusToneMapProp : getVenueWallStatusToneMap();
  var isDark = appearance === 'dark';
  var idleMain = isDark ? '#e2e8f0' : '#0f172a';
  var idleSub = isDark ? '#94a3b8' : '#94a3b8';
  var _useDriftMinutesFromE = useDriftMinutesFromEpoch(card.remainingMin, card.resourceId, wallNowMs, true),
    liveRemainingMin = _useDriftMinutesFromE.whole,
    liveRemainingPreciseMin = _useDriftMinutesFromE.precise;
  var _useDriftMinutesFromE2 = useDriftMinutesFromEpoch(card.startsInMin, card.resourceId, wallNowMs, false),
    liveStartsInMin = _useDriftMinutesFromE2.whole;
  var liveLateMin = useDriftAccumulatedMinutesFromEpoch(card.lateMin, card.resourceId, wallNowMs);
  var presentation = useMemo(function () {
    return buildVenueWallCardPresentation({
      card: card,
      toneMap: toneMap,
      wallNowMs: wallNowMs,
      liveRemainingMin: liveRemainingMin,
      liveRemainingPreciseMin: liveRemainingPreciseMin,
      liveStartsInMin: liveStartsInMin,
      liveLateMin: liveLateMin,
      boardingSoonBeforeMin: boardingSoonBeforeMin,
      idleMain: idleMain,
      idleSub: idleSub,
      occupiedCardColor: occupiedCardColor,
      availableCardColor: availableCardColor
    });
  }, [card, toneMap, wallNowMs, liveRemainingMin, liveRemainingPreciseMin, liveStartsInMin, liveLateMin, boardingSoonBeforeMin, idleMain, idleSub, occupiedCardColor, availableCardColor]);
  var floorLikeStatus = presentation.floorLikeStatus,
    headerStatusKey = presentation.headerStatusKey,
    headerStatusLabel = presentation.headerStatusLabel,
    headerStatusColor = presentation.headerStatusColor,
    cardVariant = presentation.cardVariant,
    mainColor = presentation.mainColor,
    subColor = presentation.subColor,
    surfaceAccentColor = presentation.surfaceAccentColor,
    primaryEmphasisLine = presentation.primaryEmphasisLine,
    primaryEmphasisColor = presentation.primaryEmphasisColor,
    timeRangeLine = presentation.timeRangeLine,
    contactLine = presentation.contactLine,
    showProgress = presentation.showProgress,
    progress = presentation.progress,
    progressColor = presentation.progressColor,
    showEndingSoonMarquee = presentation.showEndingSoonMarquee,
    marqueeText = presentation.marqueeText,
    marqueeColor = presentation.marqueeColor,
    marqueeShellBackground = presentation.marqueeShellBackground;
  var nextItem = (_card$nextList = card.nextList) === null || _card$nextList === void 0 ? void 0 : _card$nextList[0];
  var nextTimeMeaningful = nextItem ? nextListFieldIsMeaningful(nextItem.time) : false;
  var nextContactMeaningful = nextItem ? nextListFieldIsMeaningful(nextItem.booking) : false;
  var hasNextWallSection = Boolean(nextItem && (nextTimeMeaningful || nextContactMeaningful));
  /** 当前区为「未开始」预约：{@link RecordBoardResourceWallCardModel.wallUseNextLabelHideNextRow} 为真时不渲染下方 Next 条带 */
  var showNextSection = hasNextWallSection && !card.wallUseNextLabelHideNextRow;
  var useNextAsCurrentTitle = Boolean(card.wallUseNextLabelHideNextRow);
  var nextTimeSoloLarge = nextTimeMeaningful && !nextContactMeaningful;
  var cardBg = cardVariant === 'occupied' ? "linear-gradient(180deg, ".concat(alphaColor(occupiedCardColor, 0.96), " 0%, ").concat(alphaColor(occupiedCardColor, 0.88), " 100%)") : alphaColor(surfaceAccentColor, floorLikeStatus === 'reserved' ? 0.06 : 0.03);
  var cardBorder = cardVariant === 'occupied' ? alphaColor(occupiedCardColor, 0.5) : alphaColor(surfaceAccentColor, 0.18);
  var moduleBorder = cardVariant === 'occupied' ? 'rgba(255,255,255,0.10)' : alphaColor(surfaceAccentColor, 0.2);
  var currentPanelBg = cardVariant === 'occupied' ? 'rgba(255,255,255,0.16)' : alphaColor(surfaceAccentColor, floorLikeStatus === 'reserved' ? 0.18 : 0.2);

  /**
   * 「当前预约」区块标题 / 时段 / 联系人：
   * - 占用卡：纯白
   * - 预约中（Reserved）：主色 slate 略透明，比纯黑柔和、仍明显强于 subColor
   * - 空闲等：沿用 subColor
   */
  var currentSectionInk = cardVariant === 'occupied' ? '#ffffff' : floorLikeStatus === 'reserved' ? alphaColor(idleMain, 0.78) : subColor;
  var nextPanelBg = cardVariant === 'occupied' ? 'rgba(255,255,255,0.04)' : alphaColor(surfaceAccentColor, floorLikeStatus === 'reserved' ? 0.1 : 0.08);

  /** 预约中：Next 区联系人/时段与「当前预约」区一致，用主字色半透明，避免仅 subColor 发灰 */
  var nextSectionInk = cardVariant === 'occupied' ? subColor : floorLikeStatus === 'reserved' ? alphaColor(idleMain, 0.78) : subColor;
  var resourceTitleLabel = String((_card$resource = card.resource) !== null && _card$resource !== void 0 ? _card$resource : '').trim();
  /** 与平面图同源：`form_code` → 合并行 `area`，作资源类型/表单类型展示 */
  var resourceTypeLabel = String((_card$area = card.area) !== null && _card$area !== void 0 ? _card$area : '').trim();
  var headerStatusClass = "pisell-resource-wall-card__header-status--".concat(headerStatusKey);

  /**
   * 5 态胶囊：
   * - empty / reserved：沿用旧 accent 描边样式（颜色取 tone）
   * - late：accent 卡上浮红描边 + 红字（与一档强调色同源）
   * - occupied（inUse / endingSoon）：白字白描边白底（沿用旧占用样式）
   * - expired：占用卡上以红色实心胶囊 + 白字（与倒计时红同色相）
   */
  var headerStatusStyle = function () {
    if (headerStatusKey === 'empty') {
      return {
        color: headerStatusColor
      };
    }
    if (headerStatusKey === 'reserved' || headerStatusKey === 'late') {
      return {
        color: headerStatusColor
      };
    }
    if (headerStatusKey === 'expired') {
      return {
        color: '#ffffff',
        background: headerStatusColor
      };
    }
    return {
      color: '#ffffff'
    };
  }();
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card",
    style: {
      background: cardBg,
      borderColor: cardBorder,
      boxShadow: isDark ? '0 18px 48px rgba(0,0,0,0.35)' : '0 18px 48px rgba(15,23,42,0.06)'
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__scale-root"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__grid ".concat(showNextSection ? 'pisell-resource-wall-card__grid--with-next' : 'pisell-resource-wall-card__grid--no-next')
  }, /*#__PURE__*/React.createElement("header", {
    className: "pisell-resource-wall-card__header"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__header-main"
  }, SHOW_RESOURCE_TYPE_LABEL && resourceTypeLabel ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__tier2 pisell-resource-wall-card__header-type",
    style: {
      color: subColor
    }
  }, resourceTypeLabel) : null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__header-name",
    style: {
      color: mainColor
    }
  }, resourceTitleLabel || "\xA0")), /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__header-status ".concat(headerStatusClass),
    style: headerStatusStyle
  }, headerStatusLabel)), /*#__PURE__*/React.createElement("section", {
    className: "pisell-resource-wall-card__panel".concat(cardVariant === 'occupied' ? ' pisell-resource-wall-card__panel--occupied-current' : floorLikeStatus === 'reserved' ? ' pisell-resource-wall-card__panel--reserved-current' : ''),
    style: {
      borderColor: cardVariant === 'occupied' ? 'rgba(255,255,255,0.28)' : moduleBorder,
      background: currentPanelBg,
      minHeight: 0
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__section-title".concat(cardVariant === 'occupied' ? ' pisell-resource-wall-card__section-title--occupied-current' : floorLikeStatus === 'reserved' ? ' pisell-resource-wall-card__section-title--reserved-current' : ''),
    style: {
      color: currentSectionInk
    }
  }, getText(useNextAsCurrentTitle ? 'pisell-resource-wall-card-label-next' : 'pisell-resource-wall-card-label-current')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__current-body"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__current-main"
  }, timeRangeLine ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__tier2",
    style: {
      color: currentSectionInk
    }
  }, timeRangeLine) : null, contactLine ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__tier2",
    style: {
      color: currentSectionInk
    }
  }, contactLine) : null), primaryEmphasisLine || showProgress || showEndingSoonMarquee && marqueeText || floorLikeStatus === 'empty' ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__current-footer"
  }, floorLikeStatus === 'empty' ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__current-footer-emphasis pisell-resource-wall-card__tier2",
    style: {
      color: currentSectionInk
    }
  }, getText('pisell-resource-wall-card-current-not-in-use')) : null, primaryEmphasisLine ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__current-footer-emphasis pisell-resource-wall-card__tier1",
    style: {
      color: primaryEmphasisColor
    }
  }, primaryEmphasisLine) : null, showProgress ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__progress-wrap",
    style: {
      background: cardVariant === 'occupied' ? 'rgba(255,255,255,0.14)' : 'rgba(148,163,184,0.18)'
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__progress-fill",
    style: {
      width: "".concat(progress, "%"),
      background: progressColor
    }
  })) : null, showEndingSoonMarquee && marqueeText ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__marquee-shell",
    style: {
      background: marqueeShellBackground
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__tier3",
    style: {
      color: marqueeColor
    }
  }, /*#__PURE__*/React.createElement(NoteTicker, {
    text: marqueeText,
    color: marqueeColor,
    duration: 24,
    shouldMarquee: marqueeText.length > RESOURCE_WALL_NOTE_MARQUEE_MIN_CHARS
  }))) : null) : null)), showNextSection ? /*#__PURE__*/React.createElement("section", {
    className: "pisell-resource-wall-card__panel".concat(floorLikeStatus === 'reserved' ? ' pisell-resource-wall-card__panel--reserved-next' : ''),
    style: {
      borderColor: moduleBorder,
      background: nextPanelBg,
      minHeight: 0
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__section-title".concat(floorLikeStatus === 'reserved' ? ' pisell-resource-wall-card__section-title--reserved-next' : ''),
    style: {
      color: nextSectionInk
    }
  }, getText('pisell-resource-wall-card-label-next')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__next-stack".concat(nextContactMeaningful && nextTimeMeaningful ? ' pisell-resource-wall-card__next-stack--both-lines' : '')
  }, nextContactMeaningful && nextItem ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-resource-wall-card__tier2",
    style: {
      color: nextSectionInk
    }
  }, String(nextItem.booking).trim()) : null, nextTimeMeaningful && nextItem ? /*#__PURE__*/React.createElement("div", {
    className: nextTimeSoloLarge ? 'pisell-resource-wall-card__next-time-solo' : 'pisell-resource-wall-card__tier2',
    style: {
      color: nextSectionInk
    }
  }, String(nextItem.time).trim()) : null)) : null)));
}

/** 单块场地大屏卡：漂移与文案由 {@link buildVenueWallCardPresentation} 统一计算 */
export var VenueWallCard = /*#__PURE__*/React.memo(VenueWallCardInner);
VenueWallCard.displayName = 'VenueWallCard';