import React, { useLayoutEffect } from 'react';
import { locales } from '@pisell/utils';
import { PisellRecordBoard } from '@pisell/materials';
import { ReservationFloorMapHudTitleBar } from "../floorMap/ReservationFloorMapHud";
import { ReservationHudDrawerHandle } from "../floorMap/ReservationHudDrawerHandle";
import { ReservationHudDrawerResourceRail } from "./ReservationHudDrawerResourceRail";
import { PISELL_RESERVATION_HUD_DRAWER_COLUMN_STORAGE_KEY, I18N_HUD_DRAWER_RESOURCE_CANVAS_PLACEHOLDER, I18N_HUD_TABLE_DRAWER_TITLE } from "../reservationConstants";
import { extractFilterFormRecordIds } from "../floorMap/reservationHudResourceRootBuckets";

/**
 * 临时：HUD 抽屉打开时，隐藏抽屉内的 Today 统计条（ShellFrame `belowToolbar`）
 * 与右侧资源快筛竖栏（`ReservationHudDrawerResourceRail`）。
 * 关闭抽屉仍可用 mask / 顶抓手 / ESC。恢复时改为 `false`。
 */
export var TEMP_HIDE_HUD_DRAWER_INNER_STATS_AND_RESOURCE_RAIL = true;

/**
 * 读取元素上均匀 scale（matrix(a,0,0,d,...)）；非均匀或无法解析时返回 1。
 * body 整体 scale 时 layout×scale≈视觉；要保持「相当于 98vh」的屏幕占比，应用 height: calc(98vh / scale)。
 */
function readUniformScaleFromTransform(el) {
  var t = getComputedStyle(el).transform;
  if (!t || t === 'none') return 1;
  try {
    var m = new DOMMatrixReadOnly(t);
    var sx = Math.abs(m.a);
    var sy = Math.abs(m.d);
    if (!Number.isFinite(sx) || !Number.isFinite(sy)) return 1;
    if (Math.abs(sx - sy) > 0.03) return 1;
    return sx > 1e-6 ? sx : 1;
  } catch (_unused) {
    return 1;
  }
}
/**
 * 平面图 HUD：左下角 chip + 展开抽屉（内嵌 RecordBoard + 可选资源竖栏）。
 */
export function ReservationHudDrawer(_ref) {
  var floorMapHudTableDrawer = _ref.floorMapHudTableDrawer,
    showVenueHudDock = _ref.showVenueHudDock,
    hudDrawerOpen = _ref.hudDrawerOpen,
    hudDrawerRef = _ref.hudDrawerRef,
    hudMapItems = _ref.hudMapItems,
    dataSources = _ref.dataSources,
    handleOpenHudTableDrawer = _ref.handleOpenHudTableDrawer,
    closeHudTableDrawer = _ref.closeHudTableDrawer,
    rowKey = _ref.rowKey,
    hudDrawerPagedRows = _ref.hudDrawerPagedRows,
    hudDrawerLoading = _ref.hudDrawerLoading,
    hudDrawerFilteredTotal = _ref.hudDrawerFilteredTotal,
    hudDrawerBookingPagination = _ref.hudDrawerBookingPagination,
    hudDrawerOnPageChange = _ref.hudDrawerOnPageChange,
    hudDrawerLocalSearchParams = _ref.hudDrawerLocalSearchParams,
    hudDrawerOnSearch = _ref.hudDrawerOnSearch,
    hudDrawerOnReset = _ref.hudDrawerOnReset,
    hudDrawerGrid = _ref.hudDrawerGrid,
    hudDrawerToolBar = _ref.hudDrawerToolBar,
    batchActionBar = _ref.batchActionBar,
    hudDrawerResourceRootBuckets = _ref.hudDrawerResourceRootBuckets,
    handleHudDrawerFormRecordIdsChange = _ref.handleHudDrawerFormRecordIdsChange;
  /**
   * body 带均匀 scale 时：`98vh` 布局尺寸正确，屏幕上视觉高度会 ×scale；
   * 打开抽屉时用 `calc(98vh / scale)` 对齐「视觉上约 98vh」的占位。
   */
  useLayoutEffect(function () {
    if (typeof window === 'undefined') return;
    if (!hudDrawerOpen) {
      var cleared = hudDrawerRef.current;
      if (cleared) {
        cleared.style.height = '';
        cleared.style.maxHeight = '';
      }
      return;
    }
    var el = hudDrawerRef.current;
    if (!el) return;
    var scale = readUniformScaleFromTransform(document.body);
    if (scale > 0 && Math.abs(scale - 1) > 0.001) {
      el.style.height = "calc(98vh / ".concat(scale, ")");
      el.style.maxHeight = "calc(98vh / ".concat(scale, ")");
    } else {
      el.style.height = '';
      el.style.maxHeight = '';
    }
    return function () {
      var next = hudDrawerRef.current;
      if (next) {
        next.style.height = '';
        next.style.maxHeight = '';
      }
    };
  }, [hudDrawerOpen, hudDrawerRef]);
  if (!showVenueHudDock) return null;
  return /*#__PURE__*/React.createElement(React.Fragment, null, !hudDrawerOpen ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-hud-title",
    role: "button",
    tabIndex: 0,
    "aria-label": locales.getText(I18N_HUD_TABLE_DRAWER_TITLE),
    onClick: handleOpenHudTableDrawer,
    onKeyDown: function onKeyDown(e) {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        handleOpenHudTableDrawer();
      }
    }
  }, /*#__PURE__*/React.createElement(ReservationFloorMapHudTitleBar, {
    items: hudMapItems,
    dataSources: dataSources,
    actionMode: "expand",
    onAction: handleOpenHudTableDrawer,
    embedded: true,
    className: "pisell-reservation-hud-title-inner"
  })) : null, hudDrawerOpen ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-hud-drawer-mask",
    onClick: closeHudTableDrawer,
    "aria-hidden": "true"
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-hud-drawer",
    role: "dialog",
    "aria-modal": "false",
    "aria-label": locales.getText(I18N_HUD_TABLE_DRAWER_TITLE),
    ref: hudDrawerRef
  }, /*#__PURE__*/React.createElement(ReservationHudDrawerHandle, {
    onClose: closeHudTableDrawer,
    drawerRef: hudDrawerRef
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-hud-drawer-record-board"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-hud-drawer-split"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-hud-drawer-split-main"
  }, /*#__PURE__*/React.createElement(PisellRecordBoard, {
    layoutVariant: "phone",
    columnVisibilityStorageKey: PISELL_RESERVATION_HUD_DRAWER_COLUMN_STORAGE_KEY,
    rowKey: rowKey,
    data: hudDrawerPagedRows,
    loading: hudDrawerLoading,
    total: hudDrawerFilteredTotal,
    pagination: hudDrawerBookingPagination,
    onPageChange: hudDrawerOnPageChange,
    searchParams: hudDrawerLocalSearchParams,
    onSearch: hudDrawerOnSearch,
    onReset: hudDrawerOnReset,
    grid: hudDrawerGrid,
    toolBar: hudDrawerToolBar,
    batchActionBar: batchActionBar,
    defaultBodyView: "grid",
    bodyViewSwitchable: false,
    lockedBodyView: "grid"
  }, /*#__PURE__*/React.createElement(PisellRecordBoard.ShellFrame, {
    belowToolbar: TEMP_HIDE_HUD_DRAWER_INNER_STATS_AND_RESOURCE_RAIL ? undefined : /*#__PURE__*/React.createElement(ReservationFloorMapHudTitleBar, {
      items: hudMapItems,
      dataSources: dataSources,
      actionMode: "collapse",
      onAction: closeHudTableDrawer,
      embedded: true,
      className: "pisell-reservation-hud-drawer-stats-inner"
    })
  }, /*#__PURE__*/React.createElement(PisellRecordBoard.GridLayout, null)))), floorMapHudTableDrawer && !TEMP_HIDE_HUD_DRAWER_INNER_STATS_AND_RESOURCE_RAIL ? /*#__PURE__*/React.createElement(ReservationHudDrawerResourceRail, {
    className: "pisell-reservation-hud-drawer-resource-rail",
    buckets: hudDrawerResourceRootBuckets,
    value: extractFilterFormRecordIds(hudDrawerLocalSearchParams),
    onChange: handleHudDrawerFormRecordIdsChange,
    ariaLabel: locales.getText(I18N_HUD_DRAWER_RESOURCE_CANVAS_PLACEHOLDER)
  }) : null)))) : null);
}