var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellFloorMapLayout/utils/floorMapElementKindUtils.ts
var floorMapElementKindUtils_exports = {};
__export(floorMapElementKindUtils_exports, {
  getElementKindConfig: () => getElementKindConfig,
  getSceneElementRenderIssue: () => getSceneElementRenderIssue,
  getSwappableElementKindsInSameCategory: () => getSwappableElementKindsInSameCategory,
  isFloorMapBuiltinShapeElementKind: () => isFloorMapBuiltinShapeElementKind,
  isFloorMapImageElementKind: () => isFloorMapImageElementKind,
  patchSceneElementForKindSwitch: () => patchSceneElementForKindSwitch,
  resolveDataBindingPolicy: () => resolveDataBindingPolicy
});
module.exports = __toCommonJS(floorMapElementKindUtils_exports);
var import_types = require("../types");
var import_utils = require("@pisell/utils");
var import_floorMapStageDefaults = require("./floorMapStageDefaults");
var import_floorMapBuiltinShapes = require("./floorMapBuiltinShapes");
function isFloorMapImageElementKind(kind) {
  return kind === import_types.FLOOR_MAP_IMAGE_ELEMENT_KIND;
}
function isFloorMapBuiltinShapeElementKind(kind) {
  return kind === import_types.FLOOR_MAP_BUILTIN_SHAPE_ELEMENT_KIND;
}
function resolveDataBindingPolicy(kind) {
  return (kind == null ? void 0 : kind.dataBindingPolicy) ?? "none";
}
function getElementKindConfig(config, elementKind) {
  var _a;
  if (!elementKind || !((_a = config == null ? void 0 : config.elementKinds) == null ? void 0 : _a.length)) return void 0;
  return config.elementKinds.find((k) => k.value === elementKind);
}
function getSwappableElementKindsInSameCategory(config, elementKind) {
  var _a, _b;
  if (!elementKind || !((_a = config.elementKinds) == null ? void 0 : _a.length)) return [];
  const rawCats = config.elementKindCategories ?? [];
  const sorted = [...rawCats].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
  const fallbackId = ((_b = sorted[0]) == null ? void 0 : _b.id) ?? "_uncat";
  const self = config.elementKinds.find((k) => k.value === elementKind);
  if (!self) return [];
  const cid = self.categoryId ?? fallbackId;
  return config.elementKinds.filter((k) => {
    if (k.showInPalette === false) return false;
    if (k.value === import_types.FLOOR_MAP_STAGE_ELEMENT_KIND) return false;
    if (k.value === import_types.FLOOR_MAP_IMAGE_ELEMENT_KIND) return false;
    if (k.value === import_types.FLOOR_MAP_BUILTIN_SHAPE_ELEMENT_KIND) return false;
    return (k.categoryId ?? fallbackId) === cid;
  });
}
var DEFAULT_PLACE_W = 180;
var DEFAULT_PLACE_H = 120;
var IMAGE_PLACE_W = 400;
var IMAGE_PLACE_H = 240;
var DECO_SIZE = 48;
var BUILTIN_SHAPE_PLACE_W = 96;
var BUILTIN_SHAPE_PLACE_H = 96;
function patchSceneElementForKindSwitch(config, element, nextKindValue) {
  var _a, _b, _c;
  const nextCfg = getElementKindConfig(config, nextKindValue);
  const nextPol = resolveDataBindingPolicy(nextCfg);
  const binding = element.dataBinding;
  const isNextImage = isFloorMapImageElementKind(nextKindValue);
  const isNextStage = nextKindValue === import_types.FLOOR_MAP_STAGE_ELEMENT_KIND;
  const isNextBuiltinShape = isFloorMapBuiltinShapeElementKind(nextKindValue);
  let clearBinding = false;
  if (nextPol === "none") {
    clearBinding = true;
  } else if (binding && ((_a = nextCfg == null ? void 0 : nextCfg.allowedDataSourceKeys) == null ? void 0 : _a.length)) {
    if (!nextCfg.allowedDataSourceKeys.includes(binding.dataSourceKey)) {
      clearBinding = true;
    }
  }
  const builtinDef = isNextBuiltinShape ? (0, import_floorMapBuiltinShapes.getFloorMapBuiltinShapeDef)(element.builtinShapeKey) : void 0;
  const builtinW = (builtinDef == null ? void 0 : builtinDef.defaultSize.width) ?? BUILTIN_SHAPE_PLACE_W;
  const builtinH = (builtinDef == null ? void 0 : builtinDef.defaultSize.height) ?? BUILTIN_SHAPE_PLACE_H;
  const nextW = isNextImage ? IMAGE_PLACE_W : isNextStage ? import_floorMapStageDefaults.STAGE_DECO_DEFAULT.width : isNextBuiltinShape ? builtinW : DEFAULT_PLACE_W;
  const nextH = isNextImage ? IMAGE_PLACE_H : isNextStage ? import_floorMapStageDefaults.STAGE_DECO_DEFAULT.height : isNextBuiltinShape ? builtinH : DEFAULT_PLACE_H;
  const useDecoSize = nextPol === "none" && nextKindValue !== import_types.FLOOR_MAP_STAGE_ELEMENT_KIND && !isNextImage && !isNextBuiltinShape;
  const width = useDecoSize ? Math.max(element.width ?? DECO_SIZE, DECO_SIZE) : Math.max(element.width ?? nextW, nextW);
  const height = useDecoSize ? Math.max(element.height ?? DECO_SIZE, DECO_SIZE) : Math.max(element.height ?? nextH, nextH);
  const patch = {
    elementKind: nextKindValue,
    shape: isNextStage || isNextImage || isNextBuiltinShape ? "rect" : nextCfg == null ? void 0 : nextCfg.defaultShape,
    width,
    height,
    name: ((_b = nextCfg == null ? void 0 : nextCfg.defaultPresentation) == null ? void 0 : _b.title) != null && String(nextCfg.defaultPresentation.title).trim() !== "" ? String(nextCfg.defaultPresentation.title) : element.name
  };
  if (clearBinding) {
    patch.dataBinding = void 0;
  }
  if (isNextImage) {
    patch.imageUrl = element.imageUrl ?? "";
  } else {
    patch.imageUrl = void 0;
  }
  if (isNextBuiltinShape) {
    const keep = (0, import_floorMapBuiltinShapes.getFloorMapBuiltinShapeDef)(element.builtinShapeKey);
    patch.builtinShapeKey = (keep == null ? void 0 : keep.key) ?? ((_c = import_floorMapBuiltinShapes.FLOOR_MAP_BUILTIN_SHAPES[0]) == null ? void 0 : _c.key) ?? void 0;
  } else {
    patch.builtinShapeKey = void 0;
  }
  return patch;
}
function getSceneElementRenderIssue(element, kind, dataSources) {
  const policy = resolveDataBindingPolicy(kind);
  const binding = element.dataBinding;
  if (policy === "required" && !binding) {
    return import_utils.locales.getText(
      "pisell-floor-map-layout.binding-error.required-row"
    );
  }
  if (!binding) return null;
  const keys = kind == null ? void 0 : kind.allowedDataSourceKeys;
  if ((keys == null ? void 0 : keys.length) && !keys.includes(binding.dataSourceKey)) {
    return import_utils.locales.getText("pisell-floor-map-layout.binding-error.ds-not-allowed").replace(/\{key\}/g, binding.dataSourceKey);
  }
  const rows = dataSources == null ? void 0 : dataSources[binding.dataSourceKey];
  const rid = binding.recordId != null ? String(binding.recordId).trim() : "";
  if (rid === "" || rid.startsWith("new_")) {
    return null;
  }
  const row = rows == null ? void 0 : rows.find((r) => r.id === rid);
  if (!row) {
    return null;
  }
  const expected = kind == null ? void 0 : kind.expectedRecordFields;
  if (expected == null ? void 0 : expected.length) {
    for (const f of expected) {
      if (row[f] === void 0 || row[f] === null || row[f] === "") {
        return import_utils.locales.getText("pisell-floor-map-layout.binding-error.missing-field").replace(/\{field\}/g, f);
      }
    }
  }
  return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getElementKindConfig,
  getSceneElementRenderIssue,
  getSwappableElementKindsInSameCategory,
  isFloorMapBuiltinShapeElementKind,
  isFloorMapImageElementKind,
  patchSceneElementForKindSwitch,
  resolveDataBindingPolicy
});
