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 } 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,
    className = props.className,
    _props$mapGridVisible = props.mapGridVisible,
    mapGridVisible = _props$mapGridVisible === void 0 ? false : _props$mapGridVisible,
    onToggleMapGrid = props.onToggleMapGrid,
    _props$dockChrome = props.dockChrome,
    dockChrome = _props$dockChrome === void 0 ? false : _props$dockChrome,
    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 horizontalAlign = (_config$horizontalAli = config.horizontalAlign) !== null && _config$horizontalAli !== void 0 ? _config$horizontalAli : 'start';
  /** 右下 Dock：`row-reverse` 后入口在屏缘右侧，箭头与左下镜像（展开朝内左、收起朝外右） */
  var isDockAlignEnd = horizontalAlign === 'end';
  var scalePercent = Math.round(scale * 100);
  var showTools = showReset || showFitBounds || showFullscreen || showGridToggle;
  var hasTrailingSlot = dockExpandedTrailing != null && dockExpandedTrailing !== false;
  var dockHasExpandable = dockChrome && (showTools || hasTrailingSlot);
  var showBar = showZoom || showTools || dockChrome && hasTrailingSlot;

  /** Dock：展开条是否拉宽（与 CSS max-width 过渡联动） */
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    dockExpanded = _useState2[0],
    setDockExpanded = _useState2[1];
  /** 收起动画结束后再卸载展开条，保证 max-width 能从宽缩回 0 */
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    dockPanelMounted = _useState4[0],
    setDockPanelMounted = _useState4[1];
  var dockExpandedRef = useRef(dockExpanded);
  dockExpandedRef.current = dockExpanded;
  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 handleDockSlideTransitionEnd = useCallback(function (e) {
    if (e.propertyName !== 'max-width' || e.target !== e.currentTarget) {
      return;
    }
    if (!dockExpandedRef.current) {
      setDockPanelMounted(false);
    }
  }, []);
  var handleDockExpand = useCallback(function () {
    setDockPanelMounted(true);
    requestAnimationFrame(function () {
      requestAnimationFrame(function () {
        setDockExpanded(true);
      });
    });
  }, []);
  var handleDockCollapseClick = useCallback(function () {
    clearIdleTimer();
    setDockExpanded(false);
  }, [clearIdleTimer]);
  var handleDockBarPointerDown = useCallback(function () {
    if (dockExpanded && autoCollapseDockMs > 0) {
      scheduleDockCollapse();
    }
  }, [dockExpanded, autoCollapseDockMs, scheduleDockCollapse]);
  var renderToolButtons = function renderToolButtons() {
    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();
      },
      "aria-label": locales.getText("".concat(I18N_VC, ".reset-aria"))
    }, /*#__PURE__*/React.createElement(ReloadOutlined, null)) : null, showFitBounds && api.fitBounds ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "".concat(PREFIX, "-view-controls-tool-btn"),
      onClick: function onClick() {
        return api.fitBounds();
      },
      "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, showFullscreen && api.toggleFullscreen ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "".concat(PREFIX, "-view-controls-tool-btn"),
      onClick: function onClick() {
        return api.toggleFullscreen();
      },
      "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);
  };
  var renderDockExpandableBarContents = function renderDockExpandableBarContents() {
    return /*#__PURE__*/React.createElement(React.Fragment, null, 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();
      },
      "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();
      },
      "aria-label": locales.getText("".concat(I18N_VC, ".zoom-in-aria"))
    }, /*#__PURE__*/React.createElement(PlusOutlined, null))) : null, showZoom && (showTools || hasTrailingSlot) ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-view-controls-bar-divider"),
      "aria-hidden": true
    }) : null, showTools ? renderToolButtons() : null, hasTrailingSlot ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-view-controls-dock-trailing")
    }, dockExpandedTrailing) : null, /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "".concat(PREFIX, "-view-controls-tool-btn"),
      onClick: handleDockCollapseClick,
      "aria-expanded": dockExpanded,
      "aria-label": locales.getText("".concat(I18N_VC, ".collapse-tools-aria"))
    }, isDockAlignEnd ? /*#__PURE__*/React.createElement(RightOutlined, null) : /*#__PURE__*/React.createElement(LeftOutlined, null)));
  };
  if (!showBar) {
    return null;
  }
  if (dockChrome && dockHasExpandable) {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames("".concat(PREFIX, "-view-controls"), "".concat(PREFIX, "-view-controls--dock"), horizontalAlign === 'end' && "".concat(PREFIX, "-view-controls--align-end"), className)
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-view-controls-dock-row")
    }, /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames("".concat(PREFIX, "-view-controls-dock-expand-btn"), _defineProperty({}, "".concat(PREFIX, "-view-controls-dock-expand-btn--panel-open"), dockExpanded)),
      onClick: handleDockExpand,
      "aria-expanded": dockExpanded,
      "aria-hidden": dockExpanded,
      tabIndex: dockExpanded ? -1 : 0,
      "aria-label": locales.getText("".concat(I18N_VC, ".expand-tools-aria"))
    }, isDockAlignEnd ? /*#__PURE__*/React.createElement(LeftOutlined, null) : /*#__PURE__*/React.createElement(RightOutlined, null)), dockPanelMounted ? /*#__PURE__*/React.createElement("div", {
      className: classNames("".concat(PREFIX, "-view-controls-dock-slide"), _defineProperty({}, "".concat(PREFIX, "-view-controls-dock-slide--open"), dockExpanded)),
      onTransitionEnd: handleDockSlideTransitionEnd
    }, /*#__PURE__*/React.createElement("div", {
      className: classNames("".concat(PREFIX, "-view-controls-bar"), "".concat(PREFIX, "-view-controls-bar--dock")),
      role: "group",
      onPointerDown: handleDockBarPointerDown
    }, renderDockExpandableBarContents())) : null));
  }
  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();
    },
    "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();
    },
    "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;