import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import "./shopFloorPlanTypes.js";
//#region src/components/pisellFloorMapLayout/shopFloorPlan/shopFloorPlanPure.ts
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 _objectSpread2(_objectSpread2({}, d), {}, { layout });
	};
	const top = r.data;
	const fromTop = normalize(top);
	if (fromTop) return fromTop;
	const fromNested = normalize(top && typeof top === "object" && "data" in top ? top.data : null);
	if (fromNested) return fromNested;
	return normalize(r);
}
/**
* GET by code 无平面图时后端可能返回的业务码（走 ensure → POST 创建）。
* 404：资源不存在；4004：店铺 schedule 侧「平面图不存在」等业务约定。
*/
function isShopFloorPlanEnsureCreateCode(code) {
	return code === 404 || code === "404" || code === 4004 || code === "4004";
}
function isShopFloorPlanNotFoundResponse(res) {
	if (res == null || typeof res !== "object") return false;
	const r = res;
	const code = r.code;
	if (isShopFloorPlanEnsureCreateCode(code)) return true;
	if (r.status === false && isShopFloorPlanEnsureCreateCode(code)) return true;
	const data = r.data;
	if (data && typeof data === "object" && !Array.isArray(data)) {
		if (isShopFloorPlanEnsureCreateCode(data.code)) return true;
	}
	return false;
}
function isHttpNotFoundError(err) {
	var _e$response, _e$response2;
	/** 宿主 request 在 code!==200 时常 reject 整包 `{ code: 4004 }`，无 axios response */
	if (isShopFloorPlanNotFoundResponse(err)) return true;
	const e = err;
	if ((e === null || e === void 0 || (_e$response = e.response) === null || _e$response === void 0 ? void 0 : _e$response.status) === 404) return true;
	const data = e === null || e === void 0 || (_e$response2 = e.response) === null || _e$response2 === void 0 ? void 0 : _e$response2.data;
	if (data && typeof data === "object" && !Array.isArray(data)) {
		const c = data.code;
		if (isShopFloorPlanEnsureCreateCode(c)) return true;
	}
	return false;
}
function isFloorPlanDuplicateCodeError(err) {
	return Boolean(err && typeof err === "object" && err["floorPlanDuplicateCode"] === true);
}
function parseLayoutFieldToViewConfigPatch(raw) {
	let obj = raw;
	if (typeof raw === "string") try {
		obj = JSON.parse(raw);
	} catch (_unused) {
		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) {
	var _remote$canvases, _remote$elementKinds, _remote$elementKindCa, _remote$sceneElements, _remote$edges, _remote$datasourceCom;
	return _objectSpread2(_objectSpread2(_objectSpread2({}, base), remote), {}, {
		canvasUi: _objectSpread2(_objectSpread2({}, base.canvasUi), remote.canvasUi),
		canvases: (_remote$canvases = remote.canvases) !== null && _remote$canvases !== void 0 ? _remote$canvases : base.canvases,
		elementKinds: (_remote$elementKinds = remote.elementKinds) !== null && _remote$elementKinds !== void 0 ? _remote$elementKinds : base.elementKinds,
		elementKindCategories: (_remote$elementKindCa = remote.elementKindCategories) !== null && _remote$elementKindCa !== void 0 ? _remote$elementKindCa : base.elementKindCategories,
		sceneElements: (_remote$sceneElements = remote.sceneElements) !== null && _remote$sceneElements !== void 0 ? _remote$sceneElements : base.sceneElements,
		edges: (_remote$edges = remote.edges) !== null && _remote$edges !== void 0 ? _remote$edges : base.edges,
		datasourceComponents: (_remote$datasourceCom = remote.datasourceComponents) !== null && _remote$datasourceCom !== void 0 ? _remote$datasourceCom : 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) {
		var _el$width, _el$height;
		const w = (_el$width = el.width) !== null && _el$width !== void 0 ? _el$width : 100;
		const h = (_el$height = el.height) !== null && _el$height !== void 0 ? _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) {
	var _config$sceneElements, _options$canvasWidth, _options$canvasHeight;
	const inferred = inferCanvasSizeFromScene((_config$sceneElements = config.sceneElements) !== null && _config$sceneElements !== void 0 ? _config$sceneElements : []);
	return _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
		layout: viewConfigToLayoutPayload(config),
		canvas_width: (_options$canvasWidth = options.canvasWidth) !== null && _options$canvasWidth !== void 0 ? _options$canvasWidth : inferred.w,
		canvas_height: (_options$canvasHeight = options.canvasHeight) !== null && _options$canvasHeight !== void 0 ? _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 } : {});
}
//#endregion
export { buildFloorPlanPutBody, floorMapSavedConfigToRemotePatch, inferCanvasSizeFromScene, isFloorPlanDuplicateCodeError, isHttpNotFoundError, isShopFloorPlanNotFoundResponse, mergeFloorPlanViewRemote, parseLayoutFieldToViewConfigPatch, pickFloorPlanDetail, viewConfigToLayoutPayload };
