function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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; }
/**
 * 预约平面图视口悬浮：当前画布桌位总数与各状态数量（空闲 / 预约 / 使用中等）
 */
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { DoubleLeftOutlined } from '@ant-design/icons';
import { FLOOR_MAP_IMAGE_ELEMENT_KIND, FLOOR_MAP_STAGE_ELEMENT_KIND } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { findReservationRow } from "./reservationCards";
import "./ReservationFloorMapHud.less";

/** 与 RoomCard 右上角状态标签共用文案键，保证画布统计与图元一致 */
var I18N_TOTAL = 'pisell-reservation.floor-hud.total';
var I18N_INTERACTIVE_TITLE = 'pisell-reservation.floor-hud.interactive-title';
var I18N_STATUS_EMPTY = 'pisell-reservation.room-status-empty';
var I18N_STATUS_RESERVED = 'pisell-reservation.room-status-reserved';
var I18N_STATUS_OCCUPIED = 'pisell-reservation.room-status-occupied';
var I18N_STATUS_LOCKED = 'pisell-reservation.room-status-locked';
var I18N_STATUS_UNBOUND = 'pisell-reservation.room-status-unbound';
function isSeatLikeElement(item) {
  var k = item.elementKind;
  if (!k || k === FLOOR_MAP_IMAGE_ELEMENT_KIND || k === FLOOR_MAP_STAGE_ELEMENT_KIND) {
    return false;
  }
  return true;
}
function computeHudSeatStats(items, dataSources) {
  var total = 0;
  var idle = 0;
  var booked = 0;
  var occupied = 0;
  var maintenance = 0;
  var unbound = 0;
  var ds = dataSources !== null && dataSources !== void 0 ? dataSources : {};
  var _iterator = _createForOfIteratorHelper(items),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var item = _step.value;
      if (!isSeatLikeElement(item)) continue;
      total += 1;
      var row = findReservationRow(item, ds);
      if (!row) {
        unbound += 1;
        continue;
      }
      switch (row.cardStatus) {
        case 'available':
          idle += 1;
          break;
        case 'booked':
          booked += 1;
          break;
        case 'occupied':
        case 'barBusy':
          occupied += 1;
          break;
        case 'maintenance':
          maintenance += 1;
          break;
        default:
          idle += 1;
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return {
    total: total,
    idle: idle,
    booked: booked,
    occupied: occupied,
    maintenance: maintenance,
    unbound: unbound
  };
}

/** 场控左下抽屉：无台位且未展开时可隐藏整块 */
export function reservationFloorMapHudSeatStatsTotal(items, dataSources) {
  return computeHudSeatStats(items, dataSources).total;
}
function buildHudStatRows(stats) {
  var rows = [{
    label: locales.getText(I18N_TOTAL),
    value: stats.total
  }, {
    label: locales.getText(I18N_STATUS_EMPTY),
    value: stats.idle
  }, {
    label: locales.getText(I18N_STATUS_RESERVED),
    value: stats.booked
  }];
  if (stats.occupied > 0) {
    rows.push({
      label: locales.getText(I18N_STATUS_OCCUPIED),
      value: stats.occupied
    });
  }
  if (stats.maintenance > 0) {
    rows.push({
      label: locales.getText(I18N_STATUS_LOCKED),
      value: stats.maintenance
    });
  }
  if (stats.unbound > 0) {
    rows.push({
      label: locales.getText(I18N_STATUS_UNBOUND),
      value: stats.unbound
    });
  }
  return rows;
}

/** 统计行：横向 · 分隔，与 StatsBar / TitleBar / 手机 HUD 共用结构 */
function HudSeatStatInlineRows(props) {
  return /*#__PURE__*/React.createElement(React.Fragment, null, props.rows.map(function (r, i) {
    return /*#__PURE__*/React.createElement(React.Fragment, {
      key: r.label
    }, i > 0 ? /*#__PURE__*/React.createElement("span", {
      className: "pisell-reservation-floor-map-hud-sep",
      "aria-hidden": true
    }, "\xB7") : null, /*#__PURE__*/React.createElement("span", {
      className: "pisell-reservation-floor-map-hud-inline-item"
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-reservation-floor-map-hud-label"
    }, r.label), /*#__PURE__*/React.createElement("span", {
      className: "pisell-reservation-floor-map-hud-value"
    }, r.value)));
  }));
}
/**
 * 横向状态统计 + 右侧 slot：底栏 HUD 与 Sheet 顶栏共用（方案 A：两处可同时展示相同统计）。
 */
export function ReservationFloorMapHudStatsBar(props) {
  var items = props.items,
    dataSources = props.dataSources,
    rightSlot = props.rightSlot,
    _props$hideWhenNoSeat = props.hideWhenNoSeats,
    hideWhenNoSeats = _props$hideWhenNoSeat === void 0 ? true : _props$hideWhenNoSeat,
    className = props.className;
  var stats = useMemo(function () {
    return computeHudSeatStats(items, dataSources);
  }, [items, dataSources]);
  var rows = useMemo(function () {
    return buildHudStatRows(stats);
  }, [stats]);
  if (hideWhenNoSeats && stats.total === 0) {
    return null;
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-reservation-floor-map-hud-stats-bar', className)
  }, stats.total > 0 ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-floor-map-hud-inline-stats"
  }, /*#__PURE__*/React.createElement(HudSeatStatInlineRows, {
    rows: rows
  })) : /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-floor-map-hud-stats-bar-spacer",
    "aria-hidden": true
  }), rightSlot);
}
/**
 * 场控：单行标题栏 = 标题 + 内联统计 + 展开/收起，与 Sheet 顶栏共用。
 */
export function ReservationFloorMapHudTitleBar(props) {
  var items = props.items,
    dataSources = props.dataSources,
    actionMode = props.actionMode,
    onAction = props.onAction,
    embedded = props.embedded,
    className = props.className;
  var stats = useMemo(function () {
    return computeHudSeatStats(items, dataSources);
  }, [items, dataSources]);
  var rows = useMemo(function () {
    return buildHudStatRows(stats);
  }, [stats]);
  if (stats.total === 0) {
    if (actionMode === 'collapse') {
      var collapseAria = locales.getText('pisell-reservation.hud-collapse-aria');
      var regionLabel = locales.getText('pisell-reservation.hud-table-drawer-title');
      return /*#__PURE__*/React.createElement("div", {
        className: classNames('pisell-reservation-floor-map-hud', 'pisell-reservation-floor-map-hud--interactive-title-bar', embedded && 'pisell-reservation-floor-map-hud--embedded', 'pisell-reservation-floor-map-hud-title-bar--minimal', className),
        role: "region",
        "aria-label": regionLabel
      }, /*#__PURE__*/React.createElement("div", {
        className: "pisell-reservation-floor-map-hud-title-bar-layout"
      }, /*#__PURE__*/React.createElement("span", {
        className: "pisell-reservation-floor-map-hud-heading"
      }, locales.getText(I18N_INTERACTIVE_TITLE)), /*#__PURE__*/React.createElement("div", {
        className: "pisell-reservation-floor-map-hud-title-bar-stats"
      }), /*#__PURE__*/React.createElement("button", {
        type: "button",
        className: "pisell-reservation-floor-map-hud-chevron-btn",
        "aria-expanded": true,
        "aria-label": collapseAria,
        onClick: function onClick(e) {
          e.stopPropagation();
          onAction();
        }
      }, /*#__PURE__*/React.createElement(DoubleLeftOutlined, {
        rotate: 90
      }))));
    }
    return null;
  }
  var actionAria = actionMode === 'expand' ? locales.getText('pisell-reservation.hud-expand-aria') : locales.getText('pisell-reservation.hud-collapse-aria');
  var regionAria = locales.getText('pisell-reservation.hud-table-drawer-title');
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-reservation-floor-map-hud', 'pisell-reservation-floor-map-hud--interactive-title-bar', embedded && 'pisell-reservation-floor-map-hud--embedded', className),
    role: "region",
    "aria-label": regionAria
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-floor-map-hud-title-bar-layout"
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-reservation-floor-map-hud-heading"
  }, locales.getText(I18N_INTERACTIVE_TITLE)), /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-floor-map-hud-title-bar-stats"
  }, /*#__PURE__*/React.createElement(HudSeatStatInlineRows, {
    rows: rows
  })), /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-reservation-floor-map-hud-chevron-btn",
    "aria-expanded": actionMode === 'collapse',
    "aria-label": actionAria,
    onClick: function onClick(e) {
      e.stopPropagation();
      onAction();
    }
  }, actionMode === 'expand' ? /*#__PURE__*/React.createElement(DoubleLeftOutlined, {
    rotate: 90
  }) : /*#__PURE__*/React.createElement(DoubleLeftOutlined, {
    rotate: -90
  }))));
}
export function ReservationFloorMapHud(props) {
  var stats = useMemo(function () {
    return computeHudSeatStats(props.items, props.dataSources);
  }, [props.items, props.dataSources]);
  if (props.interactive && props.onRequestOpenTable) {
    var expanded = Boolean(props.tableDrawerOpen);
    if (stats.total === 0 && !expanded) {
      return null;
    }
    return /*#__PURE__*/React.createElement(ReservationFloorMapHudTitleBar, {
      items: props.items,
      dataSources: props.dataSources,
      actionMode: expanded ? 'collapse' : 'expand',
      onAction: function onAction() {
        var _props$onRequestClose;
        return expanded ? (_props$onRequestClose = props.onRequestCloseTable) === null || _props$onRequestClose === void 0 ? void 0 : _props$onRequestClose.call(props) : props.onRequestOpenTable();
      }
    });
  }
  if (stats.total === 0) {
    return null;
  }
  var rows = buildHudStatRows(stats);
  if (props.phoneLayout) {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames('pisell-reservation-floor-map-hud', 'pisell-reservation-floor-map-hud--phone-inline'),
      role: "status"
    }, /*#__PURE__*/React.createElement("div", {
      className: "pisell-reservation-floor-map-hud-inline-stats"
    }, /*#__PURE__*/React.createElement(HudSeatStatInlineRows, {
      rows: rows
    })));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-floor-map-hud",
    role: "status"
  }, /*#__PURE__*/React.createElement("ul", {
    className: "pisell-reservation-floor-map-hud-list"
  }, rows.map(function (r) {
    return /*#__PURE__*/React.createElement("li", {
      key: r.label,
      className: "pisell-reservation-floor-map-hud-row"
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-reservation-floor-map-hud-label"
    }, r.label), /*#__PURE__*/React.createElement("span", {
      className: "pisell-reservation-floor-map-hud-value"
    }, r.value));
  })));
}