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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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; }
import { locales } from '@pisell/utils';
import floorMapLayoutLocales from "../locales";
import { createTranslationText } from "../../translation/utils";
import { getCurrentLocale } from "../../../utils/locale";

/**
 * 合并平面图 Tab 用多语言包（与业务侧 `locales.init` 可叠加）。
 */
export function initFloorMapLayoutLocales(engineLocale) {
  locales.init(floorMapLayoutLocales, engineLocale);
}

/**
 * 当前显示语言：优先引擎存储 `appHelper.utils.storage.get('umi_locale')`（与项目内
 * 其它物料一致，且在 LCE 渲染上下文里能拿到宿主真实语言），其次 `getCurrentLocale()`
 * （globalConfig.locale → 裸 localStorage.umi_locale → en-US）兜底。
 *
 * 注意：在 LCE iframe/渲染容器里裸 `localStorage` 常取不到 `umi_locale`，必须先走引擎存储，
 * 否则会退回 en，导致徽标与其它（走引擎存储的）文案语言不一致。
 */

/** 与 List / 预约等物料一致：storage → 宿主 getLocale → native_locale → materials 当前语言 */
export function getFloorMapDisplayLocale(engineCtx) {
  var _appHelper, _utils$storage, _utils$storage$get, _utils$getApp, _utils$getApp$getLoca;
  var utils = engineCtx === null || engineCtx === void 0 || (_appHelper = engineCtx.appHelper) === null || _appHelper === void 0 ? void 0 : _appHelper.utils;
  var fromStorage = utils === null || utils === void 0 || (_utils$storage = utils.storage) === null || _utils$storage === void 0 || (_utils$storage$get = _utils$storage.get) === null || _utils$storage$get === void 0 ? void 0 : _utils$storage$get.call(_utils$storage, 'umi_locale');
  if (typeof fromStorage === 'string' && fromStorage.trim()) return fromStorage;
  var fromApp = utils === null || utils === void 0 || (_utils$getApp = utils.getApp) === null || _utils$getApp === void 0 || (_utils$getApp = _utils$getApp.call(utils)) === null || _utils$getApp === void 0 || (_utils$getApp = _utils$getApp.locales) === null || _utils$getApp === void 0 || (_utils$getApp$getLoca = _utils$getApp.getLocale) === null || _utils$getApp$getLoca === void 0 ? void 0 : _utils$getApp$getLoca.call(_utils$getApp);
  if (typeof fromApp === 'string' && fromApp.trim()) return fromApp;
  if (typeof localStorage !== 'undefined') {
    var native = localStorage.getItem('CapacitorStorage.native_locale');
    if (native && native.trim()) return native;
  }
  return getCurrentLocale();
}

/**
 * 按当前语言（或显式传入语言）从平面图语言包取文案，**不依赖全局激活语言**
 * （避免多组件 init 竞态），缺失时回退英文，再回退 key 本身。
 */
export function getFloorMapLayoutText(id, displayLocale) {
  var _ref, _ref2, _ref3, _ref4, _maps$lang$id, _maps$lang, _maps$zhCN, _maps$zh, _maps$zhHK, _maps$en;
  var lang = locales.formatLocale(displayLocale || getCurrentLocale());
  var maps = floorMapLayoutLocales;
  return (_ref = (_ref2 = (_ref3 = (_ref4 = (_maps$lang$id = (_maps$lang = maps[lang]) === null || _maps$lang === void 0 ? void 0 : _maps$lang[id]) !== null && _maps$lang$id !== void 0 ? _maps$lang$id : (_maps$zhCN = maps['zh-CN']) === null || _maps$zhCN === void 0 ? void 0 : _maps$zhCN[id]) !== null && _ref4 !== void 0 ? _ref4 : (_maps$zh = maps.zh) === null || _maps$zh === void 0 ? void 0 : _maps$zh[id]) !== null && _ref3 !== void 0 ? _ref3 : (_maps$zhHK = maps['zh-HK']) === null || _maps$zhHK === void 0 ? void 0 : _maps$zhHK[id]) !== null && _ref2 !== void 0 ? _ref2 : (_maps$en = maps.en) === null || _maps$en === void 0 ? void 0 : _maps$en[id]) !== null && _ref !== void 0 ? _ref : id;
}
var I18N_LOCALE_ORDER = ['en', 'zh-CN', 'zh-HK', 'ja', 'pt'];

/**
 * 引擎 locale 与画布 name 对象字段对齐（如 en-US → en、zh_CN → zh-CN）。
 */
function localeKeysToTryForCanvasName(engineLocale) {
  var out = [];
  var push = function push(k) {
    if (!out.includes(k)) out.push(k);
  };
  push(engineLocale);
  if (engineLocale === 'en-US' || engineLocale === 'en-GB') {
    push('en');
  }
  if (engineLocale === 'zh_CN' || engineLocale === 'zh') {
    push('zh-CN');
  }
  var _iterator = _createForOfIteratorHelper(I18N_LOCALE_ORDER),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var _k = _step.value;
      push(_k);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return out;
}

/**
 * 从多语言对象中取当前 locale 下展示字符串，缺省按 en → 其余首个非空。
 */
export function pickFloorMapCanvasI18nString(obj, engineLocale) {
  var _iterator2 = _createForOfIteratorHelper(localeKeysToTryForCanvasName(engineLocale)),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var loc = _step2.value;
      var _v = obj[loc];
      if (typeof _v === 'string' && _v.trim()) return _v.trim();
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  for (var _i = 0, _Object$values = Object.values(obj); _i < _Object$values.length; _i++) {
    var v = _Object$values[_i];
    if (typeof v === 'string' && v.trim()) return v.trim();
  }
  return '';
}
function isCanvasNameI18nObject(n) {
  if (!n || _typeof(n) !== 'object' || Array.isArray(n)) return false;
  var o = n;
  var _iterator3 = _createForOfIteratorHelper(I18N_LOCALE_ORDER),
    _step3;
  try {
    for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
      var _k2 = _step3.value;
      if (typeof o[_k2] === 'string') return true;
    }
  } catch (err) {
    _iterator3.e(err);
  } finally {
    _iterator3.f();
  }
  return false;
}

/**
 * 画布在 Tab / 下拉中的展示名：
 * 1. `name` 为多语言对象时 **优先** 按引擎 locale 取字段（与配置内嵌文案一致）；
 * 2. 否则 `nameKey` + `locales.getText`（需业务侧已 init 对应语言包）；
 * 3. 否则 `name` 字符串。
 */
export function getFloorMapCanvasDisplayName(canvas, engineLocale) {
  initFloorMapLayoutLocales(engineLocale);
  var n = canvas.name;
  if (isCanvasNameI18nObject(n)) {
    var fromObj = pickFloorMapCanvasI18nString(n, engineLocale);
    if (fromObj) return fromObj;
  }
  if (canvas.nameKey) {
    var t = locales.getText(canvas.nameKey);
    if (t !== canvas.nameKey) return t;
  }
  if (typeof n === 'string') return n;
  if (n && _typeof(n) === 'object' && !Array.isArray(n)) {
    return pickFloorMapCanvasI18nString(n, engineLocale);
  }
  return '';
}

/**
 * 打开重命名弹层时的初始多语言值（与 Translation 表单一致）。
 */
export function seedFloorMapCanvasNameI18n(canvas, displayFallback) {
  var n = canvas.name;
  if (n && _typeof(n) === 'object') {
    var _n$en, _n$zhCN, _n$zhHK, _n$ja, _n$pt;
    return {
      en: (_n$en = n.en) !== null && _n$en !== void 0 ? _n$en : '',
      'zh-CN': (_n$zhCN = n['zh-CN']) !== null && _n$zhCN !== void 0 ? _n$zhCN : '',
      'zh-HK': (_n$zhHK = n['zh-HK']) !== null && _n$zhHK !== void 0 ? _n$zhHK : '',
      ja: (_n$ja = n.ja) !== null && _n$ja !== void 0 ? _n$ja : '',
      pt: (_n$pt = n.pt) !== null && _n$pt !== void 0 ? _n$pt : ''
    };
  }
  var s = typeof n === 'string' && n.trim() ? n.trim() : displayFallback;
  return createTranslationText(s, true);
}