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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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; }
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { useMemoizedFn } from 'ahooks';
import { SecondaryScreenSettingsModal, DEFAULT_SECONDARY_DISPLAY_SETTINGS, getStoredSecondaryDisplaySettings, hasPersistedSecondaryDisplaySettings, persistSecondaryDisplaySettings } from "./ScaleModal";

/** 与 unifiedCommunication 运行时中副屏标识一致 */

/**
 * 低代码/宿主传入的副屏展示兜底（仅当本地尚未保存过完整 JSON 时参与合并）。
 * 缩放比例始终以弹窗与本地存储为准。
 */

var MAIN_CONTENT_TAP_WINDOW_MS = 800;
var MAIN_CONTENT_TAP_COUNT = 5;
/**
 * 票务预约副屏：角色判断、主内容区 transform、连点打开设置弹窗、本地持久化展示配置。
 */
export function useTicketBookingSecondaryScreen(runtime, displayConfig) {
  var isSecondaryScreen = useMemo(function () {
    return (runtime === null || runtime === void 0 ? void 0 : runtime.role) === 'secondary-screen';
  }, [runtime]);
  var _useState = useState(function () {
      return (runtime === null || runtime === void 0 ? void 0 : runtime.role) === 'secondary-screen' ? getStoredSecondaryDisplaySettings(displayConfig) : DEFAULT_SECONDARY_DISPLAY_SETTINGS;
    }),
    _useState2 = _slicedToArray(_useState, 2),
    secondaryDisplaySettings = _useState2[0],
    setSecondaryDisplaySettings = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    settingsModalOpen = _useState4[0],
    setSettingsModalOpen = _useState4[1];

  /**
   * 主屏：不读副屏 localStorage，固定默认配置并关闭设置弹窗。
   * 副屏：已持久化则只读存储；否则存储迁移 + props 兜底，且随 props 更新。
   */
  useEffect(function () {
    if (!isSecondaryScreen) {
      setSecondaryDisplaySettings(DEFAULT_SECONDARY_DISPLAY_SETTINGS);
      setSettingsModalOpen(false);
      return;
    }
    if (hasPersistedSecondaryDisplaySettings()) {
      setSecondaryDisplaySettings(getStoredSecondaryDisplaySettings());
      return;
    }
    setSecondaryDisplaySettings(getStoredSecondaryDisplaySettings(displayConfig));
  }, [isSecondaryScreen, displayConfig === null || displayConfig === void 0 ? void 0 : displayConfig.productsPerRow, displayConfig === null || displayConfig === void 0 ? void 0 : displayConfig.showCustomerInfo]);
  var secondaryScaleTapRef = useRef({
    count: 0,
    resetTimer: null
  });
  useEffect(function () {
    return function () {
      var resetTimer = secondaryScaleTapRef.current.resetTimer;
      if (resetTimer) clearTimeout(resetTimer);
    };
  }, []);
  var secondaryProductsPerRow = useMemo(function () {
    if (!isSecondaryScreen) return 1;
    return secondaryDisplaySettings.productsPerRow;
  }, [isSecondaryScreen, secondaryDisplaySettings.productsPerRow]);
  var showSecondaryCustomerInfo = useMemo(function () {
    if (!isSecondaryScreen) return true;
    return secondaryDisplaySettings.showCustomerInfo;
  }, [isSecondaryScreen, secondaryDisplaySettings.showCustomerInfo]);
  var bookingInfoModules = useMemo(function () {
    return {
      client: isSecondaryScreen && !showSecondaryCustomerInfo ? 0 : 1,
      date: 0,
      service: 1,
      holder: 1,
      note: 1
    };
  }, [isSecondaryScreen, showSecondaryCustomerInfo]);
  var transformStyle = useMemo(function () {
    if (!isSecondaryScreen) {
      return {
        width: 400
      };
    }
    var scale = secondaryDisplaySettings.scale;
    return {
      flex: " 0 0 ".concat(100 / scale, "%"),
      height: "".concat(100 / scale, "%"),
      transform: "scale(".concat(scale, ")"),
      transformOrigin: 'top left'
    };
  }, [isSecondaryScreen, secondaryDisplaySettings.scale]);
  var handleMainContentClick = useMemoizedFn(function () {
    if (!isSecondaryScreen) return;
    var box = secondaryScaleTapRef.current;
    if (box.resetTimer) {
      clearTimeout(box.resetTimer);
      box.resetTimer = null;
    }
    box.count += 1;
    if (box.count >= MAIN_CONTENT_TAP_COUNT) {
      box.count = 0;
      setSettingsModalOpen(true);
      return;
    }
    box.resetTimer = setTimeout(function () {
      box.count = 0;
      box.resetTimer = null;
    }, MAIN_CONTENT_TAP_WINDOW_MS);
  });
  var handleSettingsConfirm = useMemoizedFn(function (next) {
    if (!isSecondaryScreen) return;
    setSecondaryDisplaySettings(next);
    persistSecondaryDisplaySettings(next);
    setSettingsModalOpen(false);
  });
  var mainContentWrapperProps = useMemo(function () {
    return {
      id: 'ticket-booking-main-content',
      className: 'ticket-booking-main-content',
      style: _objectSpread({
        flexShrink: 0
      }, transformStyle),
      onClick: handleMainContentClick
    };
  }, [transformStyle, handleMainContentClick]);
  var secondaryScreenSettingsModal = isSecondaryScreen ? /*#__PURE__*/React.createElement(SecondaryScreenSettingsModal, {
    open: settingsModalOpen,
    settings: secondaryDisplaySettings,
    onCancel: function onCancel() {
      return setSettingsModalOpen(false);
    },
    onConfirm: handleSettingsConfirm
  }) : null;
  return {
    isSecondaryScreen: isSecondaryScreen,
    secondaryDisplaySettings: secondaryDisplaySettings,
    secondaryProductsPerRow: secondaryProductsPerRow,
    showSecondaryCustomerInfo: showSecondaryCustomerInfo,
    bookingInfoModules: bookingInfoModules,
    mainContentWrapperProps: mainContentWrapperProps,
    secondaryScreenSettingsModal: secondaryScreenSettingsModal
  };
}