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/shopFloorPlan/shopFloorPlanPure.ts
var shopFloorPlanPure_exports = {};
__export(shopFloorPlanPure_exports, {
  buildFloorPlanPutBody: () => buildFloorPlanPutBody,
  floorMapSavedConfigToRemotePatch: () => floorMapSavedConfigToRemotePatch,
  inferCanvasSizeFromScene: () => inferCanvasSizeFromScene,
  isFloorPlanDuplicateCodeError: () => isFloorPlanDuplicateCodeError,
  isHttpNotFoundError: () => isHttpNotFoundError,
  isShopFloorPlanNotFoundResponse: () => isShopFloorPlanNotFoundResponse,
  mergeFloorPlanViewRemote: () => mergeFloorPlanViewRemote,
  parseLayoutFieldToViewConfigPatch: () => parseLayoutFieldToViewConfigPatch,
  pickFloorPlanDetail: () => pickFloorPlanDetail,
  viewConfigToLayoutPayload: () => viewConfigToLayoutPayload
});
module.exports = __toCommonJS(shopFloorPlanPure_exports);
var import_shopFloorPlanTypes = require("./shopFloorPlanTypes");
function pickFloorPlanDetail(res) {
  const r = res;
  if (!r || typeof r !== "object") return null;
  const normalize = (node) => {
    if (!node || typeof node !== "object" || Array.isArray(node)) return null;
    const d = node;
    const idRaw = d.id;
    if (typeof idRaw !== "number" || !Number.isFinite(idRaw)) return null;
    const hasLayout = d.layout !== void 0 && d.layout !== null;
    const hasLayouts = d.layouts !== void 0 && d.layouts !== null && (typeof d.layouts === "object" || typeof d.layouts === "string");
    if (!hasLayout && !hasLayouts) return null;
    const layout = hasLayout ? d.layout : d.layouts;
    return {
      ...d,
      layout
    };
  };
  const top = r.data;
  const fromTop = normalize(top);
  if (fromTop) return fromTop;
  const nested = top && typeof top === "object" && "data" in top ? top.data : null;
  const fromNested = normalize(nested);
  if (fromNested) return fromNested;
  return normalize(r);
}
function isShopFloorPlanNotFoundResponse(res) {
  if (res == null || typeof res !== "object") return false;
  const r = res;
  const code = r.code;
  if (code === 404 || code === "404") return true;
  if (r.status === false && (code === 404 || code === "404")) return true;
  const data = r.data;
  if (data && typeof data === "object" && !Array.isArray(data)) {
    const d = data;
    if (d.code === 404 || d.code === "404") return true;
  }
  return false;
}
function isHttpNotFoundError(err) {
  var _a, _b;
  const e = err;
  if (((_a = e == null ? void 0 : e.response) == null ? void 0 : _a.status) === 404) return true;
  const data = (_b = e == null ? void 0 : e.response) == null ? void 0 : _b.data;
  if (data && typeof data === "object" && !Array.isArray(data)) {
    const c = data.code;
    if (c === 404 || c === "404") return true;
  }
  return false;
}
function isFloorPlanDuplicateCodeError(err) {
  return Boolean(
    err && typeof err === "object" && err[import_shopFloorPlanTypes.SHOP_FLOOR_PLAN_DUPLICATE_CODE] === true
  );
}
function parseLayoutFieldToViewConfigPatch(raw) {
  let obj = raw;
  if (typeof raw === "string") {
    try {
      obj = JSON.parse(raw);
    } catch {
      return null;
    }
  }
  if (!obj || typeof obj !== "object" || Array.isArray(obj)) return null;
  const o = obj;
  if (!Array.isArray(o.sceneElements)) return null;
  const patch = {
    sceneElements: o.sceneElements
  };
  if (Array.isArray(o.canvases)) {
    patch.canvases = o.canvases;
  }
  if (o.canvasUi && typeof o.canvasUi === "object" && !Array.isArray(o.canvasUi)) {
    patch.canvasUi = o.canvasUi;
  }
  if (Array.isArray(o.elementKinds)) {
    patch.elementKinds = o.elementKinds;
  }
  if (Array.isArray(o.elementKindCategories)) {
    patch.elementKindCategories = o.elementKindCategories;
  }
  if (Array.isArray(o.edges)) {
    patch.edges = o.edges;
  }
  if (o.datasourceComponents && typeof o.datasourceComponents === "object") {
    patch.datasourceComponents = o.datasourceComponents;
  }
  return patch;
}
function floorMapSavedConfigToRemotePatch(config) {
  return {
    sceneElements: config.sceneElements,
    canvases: config.canvases,
    canvasUi: config.canvasUi,
    elementKinds: config.elementKinds,
    elementKindCategories: config.elementKindCategories,
    edges: config.edges,
    datasourceComponents: config.datasourceComponents
  };
}
function mergeFloorPlanViewRemote(base, remote) {
  return {
    ...base,
    ...remote,
    canvasUi: {
      ...base.canvasUi,
      ...remote.canvasUi
    },
    canvases: remote.canvases ?? base.canvases,
    elementKinds: remote.elementKinds ?? base.elementKinds,
    elementKindCategories: remote.elementKindCategories ?? base.elementKindCategories,
    sceneElements: remote.sceneElements ?? base.sceneElements,
    edges: remote.edges ?? base.edges,
    datasourceComponents: remote.datasourceComponents ?? base.datasourceComponents
  };
}
function viewConfigToLayoutPayload(config) {
  return JSON.parse(JSON.stringify(config));
}
function inferCanvasSizeFromScene(sceneElements, fallbackWidth = 1920, fallbackHeight = 1080, padding = 80) {
  if (!sceneElements.length) {
    return { w: fallbackWidth, h: fallbackHeight };
  }
  let maxR = 0;
  let maxB = 0;
  for (const el of sceneElements) {
    const w = el.width ?? 100;
    const h = el.height ?? 100;
    maxR = Math.max(maxR, el.x + w);
    maxB = Math.max(maxB, el.y + h);
  }
  return {
    w: Math.max(fallbackWidth, Math.ceil(maxR + padding)),
    h: Math.max(fallbackHeight, Math.ceil(maxB + padding))
  };
}
function buildFloorPlanPutBody(config, options) {
  const scene = config.sceneElements ?? [];
  const inferred = inferCanvasSizeFromScene(scene);
  return {
    layout: viewConfigToLayoutPayload(config),
    canvas_width: options.canvasWidth ?? inferred.w,
    canvas_height: options.canvasHeight ?? inferred.h,
    ...options.code !== void 0 ? { code: options.code } : {},
    ...options.name !== void 0 ? { name: options.name } : {},
    ...options.sort !== void 0 ? { sort: options.sort } : {},
    ...options.status !== void 0 ? { status: options.status } : {}
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildFloorPlanPutBody,
  floorMapSavedConfigToRemotePatch,
  inferCanvasSizeFromScene,
  isFloorPlanDuplicateCodeError,
  isHttpNotFoundError,
  isShopFloorPlanNotFoundResponse,
  mergeFloorPlanViewRemote,
  parseLayoutFieldToViewConfigPatch,
  pickFloorPlanDetail,
  viewConfigToLayoutPayload
});
