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 _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); }
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; }
/**
 * ViewControls - 内置视图控制：默认仅缩放；可选复位、区域定位、全屏等
 * 平面图（dockChrome）：Dock 折叠入口 + 展开条（含业务插槽、空闲自动收起）
 * 非平面图：单条横栏，缩放与工具始终同排（无 Menu 折叠）
 */
import React, { useState, useRef, useEffect, useCallback } from 'react';
import classNames from 'classnames';
import { MinusOutlined, PlusOutlined, FullscreenOutlined, FullscreenExitOutlined, BorderInnerOutlined, LeftOutlined, RightOutlined, ReloadOutlined, ExpandOutlined, LockOutlined, UnlockOutlined } from '@ant-design/icons';
import { locales } from '@pisell/utils';
import "./ViewControls.less";
var PREFIX = 'pisell-floor-map-layout';
var I18N_VC = 'pisell-floor-map-layout.view-controls';

/** 从 react-zoom-pan-pinch 拿到的控制 API（由主组件传入） */

export function ViewControls(props) {
  var _config$horizontalAli;
  var api = props.api,
    _props$config = props.config,
    config = _props$config === void 0 ? {} : _props$config,
    _props$scale = props.scale,
    scale = _props$scale === void 0 ? 1 : _props$scale,
    _props$isFullscreen = props.isFullscreen,
    isFullscreen = _props$isFullscreen === void 0 ? false : _props$isFullscreen,
    _props$fitBoundsActiv = props.fitBoundsActive,
    fitBoundsActive = _props$fitBoundsActiv === void 0 ? false : _props$fitBoundsActiv,
    className = props.className,
    _props$mapGridVisible = props.mapGridVisible,
    mapGridVisible = _props$mapGridVisible === void 0 ? false : _props$mapGridVisible,
    onToggleMapGrid = props.onToggleMapGrid,
    _props$viewportLocked = props.viewportLocked,
    viewportLocked = _props$viewportLocked === void 0 ? false : _props$viewportLocked,
    onToggleViewportLock = props.onToggleViewportLock,
    _props$dockChrome = props.dockChrome,
    dockChrome = _props$dockChrome === void 0 ? false : _props$dockChrome,
    dockExpandedToolButtons = props.dockExpandedToolButtons,
    dockExpandedTrailing = props.dockExpandedTrailing,
    _props$autoCollapseDo = props.autoCollapseDockMs,
    autoCollapseDockMs = _props$autoCollapseDo === void 0 ? 10000 : _props$autoCollapseDo;
  var showZoom = config.showZoom !== false;
  var showReset = config.showReset === true;
  var showFitBounds = config.showFitBounds === true;
  var showFullscreen = config.showFullscreen === true;
  var showGridToggle = config.showGridToggle === true && typeof onToggleMapGrid === 'function';
  var showViewportLock = config.showViewportLock === true && typeof onToggleViewportLock === 'function';
  /**
   * `horizontalAlign` 仅作用于非 dockChrome 的普通横条（决定贴左下还是右下）。
   * dockChrome（平面图）模式已统一固定在屏右下，不再读这个配置。
   */
  var horizontalAlign = (_config$horizontalAli = config.horizontalAlign) !== null && _config$horizontalAli !== void 0 ? _config$horizontalAli : 'start';
  var scalePercent = Math.round(scale * 100);
  var showTools = showReset || showFitBounds || showFullscreen || showGridToggle || showViewportLock;
  var hasDockToolButtons = dockExpandedToolButtons != null && dockExpandedToolButtons !== false;
  var hasTrailingSlot = dockExpandedTrailing != null && dockExpandedTrailing !== false;
  var dockHasExpandable = dockChrome && (showTools || hasDockToolButtons || hasTrailingSlot);
  var showBar = showZoom || showTools || dockChrome && (hasDockToolButtons || hasTrailingSlot);

  /**
   * Dock：展开条是否拉宽（与 CSS max-width 过渡联动）。
   * dockChrome 下 slide 内嵌合并 toggle 按钮，slide 始终 mounted；
   * 折叠态 max-width 仅露 toggle 宽，展开态拉满。
   */
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    dockExpanded = _useState2[0],
    setDockExpanded = _useState2[1];
  var idleTimerRef = useRef(null);
  var clearIdleTimer = useCallback(function () {
    if (idleTimerRef.current != null) {
      clearTimeout(idleTimerRef.current);
      idleTimerRef.current = null;
    }
  }, []);
  var scheduleDockCollapse = useCallback(function () {
    clearIdleTimer();
    if (!dockChrome || !dockHasExpandable || !dockExpanded || autoCollapseDockMs <= 0) {
      return;
    }
    idleTimerRef.current = setTimeout(function () {
      setDockExpanded(false);
      idleTimerRef.current = null;
    }, autoCollapseDockMs);
  }, [dockChrome, dockHasExpandable, dockExpanded, autoCollapseDockMs, clearIdleTimer]);
  useEffect(function () {
    return function () {
      return clearIdleTimer();
    };
  }, [clearIdleTimer]);
  useEffect(function () {
    if (dockExpanded && dockHasExpandable) {
      scheduleDockCollapse();
    }
  }, [dockExpanded, dockHasExpandable, scheduleDockCollapse]);
  var handleDockExpand = useCallback(function () {
    setDockExpanded(true);
  }, []);
  var handleDockCollapseClick = useCallback(function () {
    clearIdleTimer();
    setDockExpanded(false);
  }, [clearIdleTimer]);
  var handleDockBarPointerDown = useCallback(function () {
    if (dockExpanded && autoCollapseDockMs > 0) {
      scheduleDockCollapse();
    }
  }, [dockExpanded, autoCollapseDockMs, scheduleDockCollapse]);

  /**
   * dockChrome 下「锁 / 全览」常驻在 `dock-collapsed-extras` 屏右缘，展开条内不再重复渲染。
   * 非 dockChrome 普通横条不省略，所有按钮顺排。
   */
  var renderToolButtons = function renderToolButtons(opts) {
    var omitLock = (opts === null || opts === void 0 ? void 0 : opts.omitViewportLock) === true;
    var omitFit = (opts === null || opts === void 0 ? void 0 : opts.omitFitBounds) === true;
    return /*#__PURE__*/React.createElement(React.Fragment, null, showReset ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "".concat(PREFIX, "-view-controls-tool-btn"),
      onClick: function onClick() {
        return api.resetTransform();
      },
      disabled: viewportLocked,
      "aria-disabled": viewportLocked,
      "aria-label": locales.getText("".concat(I18N_VC, ".reset-aria"))
    }, /*#__PURE__*/React.createElement(ReloadOutlined, null)) : null, showFitBounds && api.fitBounds && !omitFit ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames("".concat(PREFIX, "-view-controls-tool-btn"), _defineProperty({}, "".concat(PREFIX, "-view-controls-tool-btn--active"), fitBoundsActive)),
      onClick: function onClick() {
        return api.fitBounds();
      },
      disabled: viewportLocked,
      "aria-disabled": viewportLocked,
      "aria-pressed": fitBoundsActive,
      "aria-label": locales.getText("".concat(I18N_VC, ".fit-bounds-aria"))
    }, /*#__PURE__*/React.createElement(ExpandOutlined, null)) : null, showGridToggle ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames("".concat(PREFIX, "-view-controls-tool-btn"), _defineProperty({}, "".concat(PREFIX, "-view-controls-tool-btn--active"), mapGridVisible)),
      onClick: function onClick() {
        return onToggleMapGrid();
      },
      "aria-pressed": mapGridVisible,
      "aria-label": locales.getText(mapGridVisible ? "".concat(I18N_VC, ".grid-hide-aria") : "".concat(I18N_VC, ".grid-show-aria"))
    }, /*#__PURE__*/React.createElement(BorderInnerOutlined, null)) : null, showViewportLock && !omitLock ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames("".concat(PREFIX, "-view-controls-tool-btn"), _defineProperty({}, "".concat(PREFIX, "-view-controls-tool-btn--active"), viewportLocked)),
      onClick: function onClick() {
        return onToggleViewportLock();
      },
      "aria-pressed": viewportLocked,
      "aria-label": locales.getText(viewportLocked ? "".concat(I18N_VC, ".viewport-unlock-aria") : "".concat(I18N_VC, ".viewport-lock-aria")),
      title: locales.getText(viewportLocked ? "".concat(I18N_VC, ".viewport-unlock-aria") : "".concat(I18N_VC, ".viewport-lock-aria"))
    }, viewportLocked ? /*#__PURE__*/React.createElement(LockOutlined, null) : /*#__PURE__*/React.createElement(UnlockOutlined, null)) : null, showFullscreen && api.toggleFullscreen ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames("".concat(PREFIX, "-view-controls-tool-btn"), _defineProperty({}, "".concat(PREFIX, "-view-controls-tool-btn--active"), isFullscreen)),
      onClick: function onClick() {
        return api.toggleFullscreen();
      },
      "aria-pressed": isFullscreen,
      "aria-label": locales.getText(isFullscreen ? "".concat(I18N_VC, ".fullscreen-exit-aria") : "".concat(I18N_VC, ".fullscreen-aria"))
    }, isFullscreen ? /*#__PURE__*/React.createElement(FullscreenExitOutlined, null) : /*#__PURE__*/React.createElement(FullscreenOutlined, null)) : null);
  };

  /**
   * dockChrome 展开条内的「工具区」按钮：锁 / 全览 已常驻在 `dock-collapsed-extras` 屏右缘，
   * 展开条内不再重复渲染，避免冗余。
   */
  var dockExpandedBarHasToolButtons = showReset || showGridToggle || showFullscreen && Boolean(api.toggleFullscreen);

  /**
   * dockChrome 合并入口：同一个 toggle 按钮承担展开/收起，常驻在 slide 内最左端。
   * - 折叠态：箭头朝左，点一下展开（slide 由 toggle 宽撑成 max-width）；
   * - 展开态：箭头朝右，点一下收起；toggle 跟随 slide max-width 收缩"被推回"右侧屏缘。
   * 主题色样式（紫底白 icon）由 `dock-toggle-btn` 类提供。
   */
  var renderDockToggleButton = function renderDockToggleButton() {
    return /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames("".concat(PREFIX, "-view-controls-dock-toggle-btn"), _defineProperty({}, "".concat(PREFIX, "-view-controls-dock-toggle-btn--panel-open"), dockExpanded)),
      onClick: dockExpanded ? handleDockCollapseClick : handleDockExpand,
      "aria-expanded": dockExpanded,
      "aria-label": locales.getText(dockExpanded ? "".concat(I18N_VC, ".collapse-tools-aria") : "".concat(I18N_VC, ".expand-tools-aria"))
    }, dockExpanded ? /*#__PURE__*/React.createElement(RightOutlined, null) : /*#__PURE__*/React.createElement(LeftOutlined, null));
  };
  var renderDockExpandableBarContents = function renderDockExpandableBarContents() {
    return /*#__PURE__*/React.createElement(React.Fragment, null, renderDockToggleButton(), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-view-controls-dock-scroll")
    }, showZoom ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "".concat(PREFIX, "-view-controls-tool-btn"),
      onClick: function onClick() {
        return api.zoomOut();
      },
      disabled: viewportLocked,
      "aria-disabled": viewportLocked,
      "aria-label": locales.getText("".concat(I18N_VC, ".zoom-out-aria"))
    }, /*#__PURE__*/React.createElement(MinusOutlined, null)), /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX, "-view-controls-zoom-value")
    }, scalePercent, "%"), /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "".concat(PREFIX, "-view-controls-tool-btn"),
      onClick: function onClick() {
        return api.zoomIn();
      },
      disabled: viewportLocked,
      "aria-disabled": viewportLocked,
      "aria-label": locales.getText("".concat(I18N_VC, ".zoom-in-aria"))
    }, /*#__PURE__*/React.createElement(PlusOutlined, null))) : null, showZoom && (dockExpandedBarHasToolButtons || hasDockToolButtons || hasTrailingSlot) ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-view-controls-bar-divider"),
      "aria-hidden": true
    }) : null, dockExpandedBarHasToolButtons ? renderToolButtons({
      omitViewportLock: true,
      omitFitBounds: true
    }) : null, hasDockToolButtons ? dockExpandedToolButtons : null, hasTrailingSlot ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-view-controls-dock-trailing")
    }, dockExpandedTrailing) : null));
  };
  if (!showBar) {
    return null;
  }

  /**
   * dockChrome 屏右缘常驻按钮：「锁 / 全览」。两者都是单击操作，与展开条解耦。
   * dockChrome 模式下 extras 与 slide 共用同一外壳，无 panel-open 收起行为。
   */
  var renderDockCollapsedExtras = function renderDockCollapsedExtras() {
    var showLockChip = showViewportLock;
    var showFitChip = showFitBounds && Boolean(api.fitBounds);
    if (!showLockChip && !showFitChip) return null;
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-view-controls-dock-collapsed-extras")
    }, showLockChip ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames("".concat(PREFIX, "-view-controls-tool-btn"), "".concat(PREFIX, "-view-controls-dock-collapsed-extras-btn"), _defineProperty({}, "".concat(PREFIX, "-view-controls-tool-btn--active"), viewportLocked)),
      onClick: function onClick() {
        return onToggleViewportLock();
      },
      "aria-pressed": viewportLocked,
      "aria-label": locales.getText(viewportLocked ? "".concat(I18N_VC, ".viewport-unlock-aria") : "".concat(I18N_VC, ".viewport-lock-aria")),
      title: locales.getText(viewportLocked ? "".concat(I18N_VC, ".viewport-unlock-aria") : "".concat(I18N_VC, ".viewport-lock-aria"))
    }, viewportLocked ? /*#__PURE__*/React.createElement(LockOutlined, null) : /*#__PURE__*/React.createElement(UnlockOutlined, null)) : null, showFitChip ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames("".concat(PREFIX, "-view-controls-tool-btn"), "".concat(PREFIX, "-view-controls-dock-collapsed-extras-btn"), _defineProperty({}, "".concat(PREFIX, "-view-controls-tool-btn--active"), fitBoundsActive)),
      onClick: function onClick() {
        return api.fitBounds();
      },
      disabled: viewportLocked,
      "aria-disabled": viewportLocked,
      "aria-pressed": fitBoundsActive,
      "aria-label": locales.getText("".concat(I18N_VC, ".fit-bounds-aria")),
      title: locales.getText("".concat(I18N_VC, ".fit-bounds-aria"))
    }, /*#__PURE__*/React.createElement(ExpandOutlined, null)) : null);
  };
  if (dockChrome && dockHasExpandable) {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames("".concat(PREFIX, "-view-controls"), "".concat(PREFIX, "-view-controls--dock"), /* dockChrome 始终右下；保留 --align-end 类以复用既有 CSS */"".concat(PREFIX, "-view-controls--align-end"), className)
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-view-controls-dock-row")
    }, renderDockCollapsedExtras(), /*#__PURE__*/React.createElement("div", {
      className: classNames("".concat(PREFIX, "-view-controls-dock-slide"), _defineProperty({}, "".concat(PREFIX, "-view-controls-dock-slide--open"), dockExpanded))
    }, /*#__PURE__*/React.createElement("div", {
      className: classNames("".concat(PREFIX, "-view-controls-bar"), "".concat(PREFIX, "-view-controls-bar--dock")),
      role: "group",
      onPointerDown: handleDockBarPointerDown
    }, renderDockExpandableBarContents()))));
  }
  var barClass = classNames("".concat(PREFIX, "-view-controls-bar"), _defineProperty({}, "".concat(PREFIX, "-view-controls-bar--dock"), dockChrome));
  return /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(PREFIX, "-view-controls"), className, _defineProperty(_defineProperty({}, "".concat(PREFIX, "-view-controls--dock"), dockChrome), "".concat(PREFIX, "-view-controls--align-end"), horizontalAlign === 'end'))
  }, /*#__PURE__*/React.createElement("div", {
    className: barClass,
    role: "group",
    onPointerDown: dockChrome ? handleDockBarPointerDown : undefined
  }, showZoom ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "".concat(PREFIX, "-view-controls-tool-btn"),
    onClick: function onClick() {
      return api.zoomOut();
    },
    disabled: viewportLocked,
    "aria-disabled": viewportLocked,
    "aria-label": locales.getText("".concat(I18N_VC, ".zoom-out-aria"))
  }, /*#__PURE__*/React.createElement(MinusOutlined, null)), /*#__PURE__*/React.createElement("span", {
    className: "".concat(PREFIX, "-view-controls-zoom-value")
  }, scalePercent, "%"), /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "".concat(PREFIX, "-view-controls-tool-btn"),
    onClick: function onClick() {
      return api.zoomIn();
    },
    disabled: viewportLocked,
    "aria-disabled": viewportLocked,
    "aria-label": locales.getText("".concat(I18N_VC, ".zoom-in-aria"))
  }, /*#__PURE__*/React.createElement(PlusOutlined, null))) : null, !dockChrome && showTools ? /*#__PURE__*/React.createElement(React.Fragment, null, showZoom ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "-view-controls-bar-divider"),
    "aria-hidden": true
  }) : null, renderToolButtons()) : null));
}
export default ViewControls;