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 _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; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { getText } from "../../../../locales";
import { ResourceWallControlPanelColorTab } from "./ResourceWallControlPanelColorTab";
import { ResourceWallControlPanelLayoutTab } from "./ResourceWallControlPanelLayoutTab";
import { diffVenueWallStatusToneOverrides, getVenueWallStatusToneMap } from "./venueWallUtils";
/** 将面板内草稿压成一次 `onApply` 负载（行列、秒数、色值等做安全钳位） */
var buildApplyPayload = function buildApplyPayload(resourceIds, draft, boardingSoonBeforeMin) {
  return {
    resourceIds: _toConsumableArray(resourceIds),
    layoutKey: draft.draftLayoutKey,
    customCols: Math.max(1, Math.min(6, draft.draftCustomCols)),
    customRows: Math.max(1, Math.min(6, draft.draftCustomRows)),
    pageDurationSec: Math.max(3, Math.min(60, Math.round(draft.draftPageSec))),
    occupiedCardColor: draft.draftOcc,
    availableCardColor: draft.draftAvail,
    boardingSoonBeforeMin: Math.max(1, Math.min(60, Math.round(boardingSoonBeforeMin))),
    statusToneOverrides: diffVenueWallStatusToneOverrides(draft.draftTones),
    heroMaxWidthRatio: Math.max(0.4, Math.min(0.8, draft.draftHeroRatio))
  };
};

/**
 * 资源墙管理台（Portal）：选资源、网格预设/自定义行列、翻页秒数、占用/空闲色、六态说明与色值；
 * `onApply` 回传负载中含 `diffVenueWallStatusToneOverrides` 压缩结果。
 * 源自 test `showAdmin` 对接；关闭（Close / Escape / 遮罩）时提交草稿并关闭。
 */
export var ResourceWallControlPanel = function ResourceWallControlPanel(_ref) {
  var open = _ref.open,
    onClose = _ref.onClose,
    isDark = _ref.isDark,
    layoutPresets = _ref.layoutPresets,
    layoutKey = _ref.layoutKey,
    customCols = _ref.customCols,
    customRows = _ref.customRows,
    pageDurationSec = _ref.pageDurationSec,
    occupiedCardColor = _ref.occupiedCardColor,
    availableCardColor = _ref.availableCardColor,
    boardingSoonBeforeMin = _ref.boardingSoonBeforeMin,
    statusToneOverrides = _ref.statusToneOverrides,
    selectedResourceIds = _ref.selectedResourceIds,
    onApply = _ref.onApply,
    carouselPager = _ref.carouselPager,
    _ref$heroMaxWidthRati = _ref.heroMaxWidthRatio,
    heroMaxWidthRatioProp = _ref$heroMaxWidthRati === void 0 ? 0.6 : _ref$heroMaxWidthRati;
  var _useState = useState('resource_layout'),
    _useState2 = _slicedToArray(_useState, 2),
    adminTab = _useState2[0],
    setAdminTab = _useState2[1];
  var _useState3 = useState(layoutKey),
    _useState4 = _slicedToArray(_useState3, 2),
    draftLayoutKey = _useState4[0],
    setDraftLayoutKey = _useState4[1];
  var _useState5 = useState(customCols),
    _useState6 = _slicedToArray(_useState5, 2),
    draftCustomCols = _useState6[0],
    setDraftCustomCols = _useState6[1];
  var _useState7 = useState(customRows),
    _useState8 = _slicedToArray(_useState7, 2),
    draftCustomRows = _useState8[0],
    setDraftCustomRows = _useState8[1];
  var _useState9 = useState(pageDurationSec),
    _useState10 = _slicedToArray(_useState9, 2),
    draftPageSec = _useState10[0],
    setDraftPageSec = _useState10[1];
  var _useState11 = useState(occupiedCardColor),
    _useState12 = _slicedToArray(_useState11, 2),
    draftOcc = _useState12[0],
    setDraftOcc = _useState12[1];
  var _useState13 = useState(availableCardColor),
    _useState14 = _slicedToArray(_useState13, 2),
    draftAvail = _useState14[0],
    setDraftAvail = _useState14[1];
  var _useState15 = useState(heroMaxWidthRatioProp),
    _useState16 = _slicedToArray(_useState15, 2),
    draftHeroRatio = _useState16[0],
    setDraftHeroRatio = _useState16[1];
  var _useState17 = useState(function () {
      return getVenueWallStatusToneMap();
    }),
    _useState18 = _slicedToArray(_useState17, 2),
    draftTones = _useState18[0],
    setDraftTones = _useState18[1];

  /**
   * 弹层打开瞬间从 props 灌入草稿；effect **只依赖 open**，避免父组件每秒重绘（时钟等）时
   * 因 layoutKey 等引用变化再次执行 effect 把版式/配色草稿刷回 props。
   */
  var draftBootstrapRef = useRef({
    layoutKey: layoutKey,
    customCols: customCols,
    customRows: customRows,
    pageDurationSec: pageDurationSec,
    occupiedCardColor: occupiedCardColor,
    availableCardColor: availableCardColor,
    statusToneOverrides: statusToneOverrides,
    heroMaxWidthRatioProp: heroMaxWidthRatioProp
  });
  draftBootstrapRef.current = {
    layoutKey: layoutKey,
    customCols: customCols,
    customRows: customRows,
    pageDurationSec: pageDurationSec,
    occupiedCardColor: occupiedCardColor,
    availableCardColor: availableCardColor,
    statusToneOverrides: statusToneOverrides,
    heroMaxWidthRatioProp: heroMaxWidthRatioProp
  };
  var hasOpenedRef = useRef(false);
  useEffect(function () {
    if (!open) {
      hasOpenedRef.current = false;
      return;
    }
    if (hasOpenedRef.current) {
      return;
    }
    hasOpenedRef.current = true;
    var b = draftBootstrapRef.current;
    setDraftLayoutKey(b.layoutKey);
    setDraftCustomCols(b.customCols);
    setDraftCustomRows(b.customRows);
    setDraftPageSec(b.pageDurationSec);
    setDraftOcc(b.occupiedCardColor);
    setDraftAvail(b.availableCardColor);
    setDraftHeroRatio(b.heroMaxWidthRatioProp);
    setDraftTones(getVenueWallStatusToneMap(b.statusToneOverrides));
    setAdminTab('resource_layout');
  }, [open]);
  var flushApplyAndClose = useCallback(function () {
    onApply(buildApplyPayload(selectedResourceIds, {
      draftLayoutKey: draftLayoutKey,
      draftCustomCols: draftCustomCols,
      draftCustomRows: draftCustomRows,
      draftPageSec: draftPageSec,
      draftOcc: draftOcc,
      draftAvail: draftAvail,
      draftTones: draftTones,
      draftHeroRatio: draftHeroRatio
    }, boardingSoonBeforeMin));
    onClose();
  }, [selectedResourceIds, draftLayoutKey, draftCustomCols, draftCustomRows, draftPageSec, draftOcc, draftAvail, draftTones, draftHeroRatio, boardingSoonBeforeMin, onApply, onClose]);

  /** Escape：与关闭按钮一致，提交当前草稿并关闭（非「取消放弃」语义） */
  useEffect(function () {
    if (!open) return;
    var onKey = function onKey(e) {
      if (e.key === 'Escape') flushApplyAndClose();
    };
    window.addEventListener('keydown', onKey);
    return function () {
      return window.removeEventListener('keydown', onKey);
    };
  }, [open, flushApplyAndClose]);
  var shellBg = isDark ? 'bg-slate-900 border-slate-800' : 'bg-white border-slate-200';
  var divider = isDark ? 'border-slate-800' : 'border-slate-200';
  var headerText = isDark ? 'text-slate-100' : 'text-slate-900';
  var mutedText = isDark ? 'text-slate-500' : 'text-slate-400';
  var adminInputBg = isDark ? 'bg-slate-900 border-slate-700 text-slate-100' : 'bg-white border-slate-200 text-slate-700';
  var adminSectionBg = isDark ? 'bg-slate-950 border-slate-800' : 'bg-slate-50 border-slate-200';
  var cur = carouselPager.currentPage + 1;
  var tot = Math.max(1, carouselPager.totalPages);
  if (!open) return null;
  var modal = /*#__PURE__*/React.createElement("div", {
    className: "fixed inset-0 z-50 bg-slate-900/40 backdrop-blur-[2px] p-4 md:p-8",
    onClick: flushApplyAndClose,
    role: "presentation"
  }, /*#__PURE__*/React.createElement("div", {
    className: "mx-auto max-w-[900px] rounded-[30px] border shadow-[0_30px_80px_rgba(15,23,42,0.16)] overflow-hidden ".concat(shellBg),
    onClick: function onClick(e) {
      return e.stopPropagation();
    },
    role: "dialog",
    "aria-modal": "true"
  }, /*#__PURE__*/React.createElement("div", {
    className: "border-b px-5 md:px-6 py-4 flex items-center justify-between gap-4 ".concat(divider)
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
    className: "text-[11px] uppercase tracking-[0.24em] ".concat(mutedText)
  }, getText('pisell-resource-wall-control-eyebrow')), /*#__PURE__*/React.createElement("div", {
    className: "mt-1 text-2xl font-semibold tracking-tight ".concat(headerText)
  }, getText('pisell-resource-wall-control-subtitle')), /*#__PURE__*/React.createElement("div", {
    className: "mt-3 inline-flex items-center rounded-full border p-1 border-slate-200 bg-white"
  }, /*#__PURE__*/React.createElement("button", {
    type: "button",
    onClick: function onClick() {
      return setAdminTab('resource_layout');
    },
    className: "rounded-full px-3 py-1.5 text-sm transition ".concat(adminTab === 'resource_layout' ? 'bg-slate-900 text-white' : 'text-slate-600 hover:bg-slate-100')
  }, getText('pisell-resource-wall-tab-layout')), /*#__PURE__*/React.createElement("button", {
    type: "button",
    onClick: function onClick() {
      return setAdminTab('color');
    },
    className: "rounded-full px-3 py-1.5 text-sm transition ".concat(adminTab === 'color' ? 'bg-slate-900 text-white' : 'text-slate-600 hover:bg-slate-100')
  }, getText('pisell-resource-wall-tab-colors')))), /*#__PURE__*/React.createElement("button", {
    type: "button",
    onClick: flushApplyAndClose,
    className: "rounded-full px-4 py-2 text-sm border ".concat(isDark ? 'border-slate-700 text-slate-300 hover:bg-slate-800' : 'border-slate-200 text-slate-600 hover:bg-slate-50')
  }, getText('pisell-resource-wall-control-close'))), /*#__PURE__*/React.createElement("div", {
    className: "p-5 md:p-6 space-y-6 max-h-[78vh] overflow-auto"
  }, adminTab === 'resource_layout' ? /*#__PURE__*/React.createElement(ResourceWallControlPanelLayoutTab, {
    adminSectionBg: adminSectionBg,
    adminInputBg: adminInputBg,
    headerText: headerText,
    mutedText: mutedText,
    layoutPresets: layoutPresets,
    draftLayoutKey: draftLayoutKey,
    setDraftLayoutKey: setDraftLayoutKey,
    draftCustomCols: draftCustomCols,
    setDraftCustomCols: setDraftCustomCols,
    draftCustomRows: draftCustomRows,
    setDraftCustomRows: setDraftCustomRows,
    draftPageSec: draftPageSec,
    setDraftPageSec: setDraftPageSec,
    draftHeroRatio: draftHeroRatio,
    setDraftHeroRatio: setDraftHeroRatio,
    carouselCur: cur,
    carouselTot: tot,
    isDark: isDark
  }) : /*#__PURE__*/React.createElement(ResourceWallControlPanelColorTab, {
    adminSectionBg: adminSectionBg,
    adminInputBg: adminInputBg,
    headerText: headerText,
    mutedText: mutedText,
    divider: divider,
    draftOcc: draftOcc,
    setDraftOcc: setDraftOcc,
    draftAvail: draftAvail,
    setDraftAvail: setDraftAvail,
    draftTones: draftTones,
    setDraftTones: setDraftTones
  }))));
  return typeof document !== 'undefined' ? /*#__PURE__*/createPortal(modal, document.body) : null;
};