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 _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; }
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); }
import React, { useEffect, useState } from 'react';
import { InputNumber, Switch } from 'antd';
import { locales } from '@pisell/utils';
import { PisellModal } from '@pisell/materials';

/** 副屏内容缩放系数：与 transform 中 scale 含义一致 */
export var SECONDARY_DISPLAY_DEFAULT_SCALE = 2.5;
export var SECONDARY_DISPLAY_MIN_SCALE = 0.25;
export var SECONDARY_DISPLAY_MAX_SCALE = 5;
export var SECONDARY_PRODUCTS_PER_ROW_MIN = 1;
export var SECONDARY_PRODUCTS_PER_ROW_MAX = 8;

/** 统一副屏展示配置（缩放 + 购物车列数 + 客户信息） */

export var DEFAULT_SECONDARY_DISPLAY_SETTINGS = {
  scale: SECONDARY_DISPLAY_DEFAULT_SCALE,
  productsPerRow: 1,
  showCustomerInfo: true
};
var SECONDARY_DISPLAY_SETTINGS_STORAGE_KEY = 'pisell-ticket-booking-secondary-display-settings';

/** 历史仅缩放键，读取后迁移到新 JSON 并可在首次保存时删除 */
var SECONDARY_DISPLAY_SCALE_STORAGE_KEY_LEGACY = 'pisell-ticket-booking-secondary-display-scale';
function clampScale(scale) {
  if (!Number.isFinite(scale)) return SECONDARY_DISPLAY_DEFAULT_SCALE;
  if (scale < SECONDARY_DISPLAY_MIN_SCALE) return SECONDARY_DISPLAY_MIN_SCALE;
  if (scale > SECONDARY_DISPLAY_MAX_SCALE) return SECONDARY_DISPLAY_MAX_SCALE;
  return scale;
}
function clampProductsPerRow(n) {
  var v = Math.floor(Number(n));
  if (!Number.isFinite(v) || v < SECONDARY_PRODUCTS_PER_ROW_MIN) {
    return SECONDARY_PRODUCTS_PER_ROW_MIN;
  }
  return Math.min(v, SECONDARY_PRODUCTS_PER_ROW_MAX);
}

/** 将任意部分字段归一化为完整配置 */
export function normalizeSecondaryDisplaySettings(partial) {
  var _partial$scale, _partial$productsPerR;
  return {
    scale: clampScale((_partial$scale = partial.scale) !== null && _partial$scale !== void 0 ? _partial$scale : DEFAULT_SECONDARY_DISPLAY_SETTINGS.scale),
    productsPerRow: clampProductsPerRow((_partial$productsPerR = partial.productsPerRow) !== null && _partial$productsPerR !== void 0 ? _partial$productsPerR : DEFAULT_SECONDARY_DISPLAY_SETTINGS.productsPerRow),
    showCustomerInfo: partial.showCustomerInfo !== false
  };
}

/** 是否已保存过完整 JSON（保存后低代码 props 不再覆盖列数/客户信息） */
export function hasPersistedSecondaryDisplaySettings() {
  try {
    if (typeof localStorage === 'undefined') return false;
    return localStorage.getItem(SECONDARY_DISPLAY_SETTINGS_STORAGE_KEY) != null;
  } catch (_unused) {
    return false;
  }
}
function readLegacyScaleOnly() {
  try {
    if (typeof localStorage === 'undefined') return null;
    var raw = localStorage.getItem(SECONDARY_DISPLAY_SCALE_STORAGE_KEY_LEGACY);
    if (raw == null) return null;
    var n = parseFloat(raw);
    if (Number.isNaN(n) || n < SECONDARY_DISPLAY_MIN_SCALE || n > SECONDARY_DISPLAY_MAX_SCALE) {
      return null;
    }
    return n;
  } catch (_unused2) {
    return null;
  }
}

/**
 * 读取副屏展示配置（仅应在副屏场景调用；主屏不应依赖此结果驱动 UI）。
 * - 若已存在新 JSON 键：仅使用本地存储（低代码 props 不覆盖）。
 * - 否则：默认 + 旧 scale 迁移 + propsFallback（每行商品数、客户信息）合并。
 */
export function getStoredSecondaryDisplaySettings(propsFallback) {
  try {
    if (typeof localStorage === 'undefined') {
      return normalizeSecondaryDisplaySettings(_objectSpread(_objectSpread({}, DEFAULT_SECONDARY_DISPLAY_SETTINGS), propsFallback));
    }
    if (hasPersistedSecondaryDisplaySettings()) {
      var raw = localStorage.getItem(SECONDARY_DISPLAY_SETTINGS_STORAGE_KEY);
      if (raw) {
        var parsed = JSON.parse(raw);
        return normalizeSecondaryDisplaySettings(parsed);
      }
    }
    var legacyScale = readLegacyScaleOnly();
    return normalizeSecondaryDisplaySettings(_objectSpread(_objectSpread(_objectSpread({}, DEFAULT_SECONDARY_DISPLAY_SETTINGS), legacyScale != null ? {
      scale: legacyScale
    } : {}), propsFallback));
  } catch (_unused3) {
    return normalizeSecondaryDisplaySettings(_objectSpread(_objectSpread({}, DEFAULT_SECONDARY_DISPLAY_SETTINGS), propsFallback));
  }
}

/** 持久化完整配置；写入后删除旧 scale 单键，避免双源 */
export function persistSecondaryDisplaySettings(settings) {
  var next = normalizeSecondaryDisplaySettings(settings);
  try {
    if (typeof localStorage !== 'undefined') {
      localStorage.setItem(SECONDARY_DISPLAY_SETTINGS_STORAGE_KEY, JSON.stringify(next));
      localStorage.removeItem(SECONDARY_DISPLAY_SCALE_STORAGE_KEY_LEGACY);
    }
  } catch (_unused4) {
    // 忽略存储失败（隐私模式等）
  }
}

/** @deprecated 请使用 getStoredSecondaryDisplaySettings().scale */
export function getStoredSecondaryDisplayScale() {
  return getStoredSecondaryDisplaySettings().scale;
}

/** @deprecated 请使用 persistSecondaryDisplaySettings 合并更新 scale */
export function persistSecondaryDisplayScale(scale) {
  var cur = getStoredSecondaryDisplaySettings();
  persistSecondaryDisplaySettings(_objectSpread(_objectSpread({}, cur), {}, {
    scale: clampScale(scale)
  }));
}
/**
 * 副屏设置弹窗：缩放、购物车每行商品数、是否显示客户信息；确认后由外层持久化。
 */
export var SecondaryScreenSettingsModal = function SecondaryScreenSettingsModal(props) {
  var open = props.open,
    settings = props.settings,
    onCancel = props.onCancel,
    onConfirm = props.onConfirm;
  var _useState = useState(settings),
    _useState2 = _slicedToArray(_useState, 2),
    draft = _useState2[0],
    setDraft = _useState2[1];
  useEffect(function () {
    if (open) {
      setDraft(normalizeSecondaryDisplaySettings(settings));
    }
  }, [open, settings]);
  var handleOk = function handleOk() {
    onConfirm(normalizeSecondaryDisplaySettings(draft));
  };
  return /*#__PURE__*/React.createElement(PisellModal, {
    open: open,
    title: locales.getText('pisell2.ticket-booking.secondary-settings-title'),
    onOk: handleOk,
    onCancel: onCancel,
    okText: locales.getText('pisell2.ticket-booking.secondary-settings-ok'),
    cancelText: locales.getText('pisell2.ticket-booking.secondary-settings-cancel'),
    destroyOnClose: true,
    size: "small"
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      marginBottom: 12
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      marginBottom: 8
    }
  }, locales.getText('pisell2.ticket-booking.secondary-settings-scale')), /*#__PURE__*/React.createElement(InputNumber, {
    style: {
      width: '100%'
    },
    min: SECONDARY_DISPLAY_MIN_SCALE,
    max: SECONDARY_DISPLAY_MAX_SCALE,
    step: 0.1,
    value: draft.scale,
    onChange: function onChange(v) {
      if (v === null || v === undefined) return;
      setDraft(function (d) {
        return _objectSpread(_objectSpread({}, d), {}, {
          scale: v
        });
      });
    }
  })), /*#__PURE__*/React.createElement("div", {
    style: {
      marginBottom: 12
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      marginBottom: 8
    }
  }, locales.getText('pisell2.ticket-booking.secondary-settings-products-per-row')), /*#__PURE__*/React.createElement(InputNumber, {
    style: {
      width: '100%'
    },
    min: SECONDARY_PRODUCTS_PER_ROW_MIN,
    max: SECONDARY_PRODUCTS_PER_ROW_MAX,
    step: 1,
    precision: 0,
    value: draft.productsPerRow,
    onChange: function onChange(v) {
      if (v === null || v === undefined) return;
      setDraft(function (d) {
        return _objectSpread(_objectSpread({}, d), {}, {
          productsPerRow: v
        });
      });
    }
  })), /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'space-between',
      gap: 12
    }
  }, /*#__PURE__*/React.createElement("span", null, locales.getText('pisell2.ticket-booking.secondary-settings-show-customer')), /*#__PURE__*/React.createElement(Switch, {
    checked: draft.showCustomerInfo,
    onChange: function onChange(checked) {
      return setDraft(function (d) {
        return _objectSpread(_objectSpread({}, d), {}, {
          showCustomerInfo: checked
        });
      });
    }
  })));
};