const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_shopFloorPlanTypes = require("./shopFloorPlanTypes.js");
const require_shopFloorPlanPure = require("./shopFloorPlanPure.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
//#region src/components/pisellFloorMapLayout/shopFloorPlan/shopFloorPlanClient.ts
const _excluded = ["buildDefaultPostBody"];
function fetchShopFloorPlanByCodeResponse(_x, _x2, _x3) {
	return _fetchShopFloorPlanByCodeResponse.apply(this, arguments);
}
function _fetchShopFloorPlanByCodeResponse() {
	_fetchShopFloorPlanByCodeResponse = require_asyncToGenerator._asyncToGenerator(function* (http, trimmedCode, subscribe) {
		const res = yield http.get(`/shop/schedule/floor-plan/code/${encodeURIComponent(trimmedCode)}`, {}, require_objectSpread2._objectSpread2({
			fullResult: true,
			isShopApi: true
		}, (subscribe === null || subscribe === void 0 ? void 0 : subscribe.subscriberId) != null && subscribe.subscriberId !== "" ? require_objectSpread2._objectSpread2({
			abort: true,
			osServer: subscribe.osServer !== false,
			subscriberId: subscribe.subscriberId
		}, subscribe.callback != null ? { callback: subscribe.callback } : {}) : {}));
		return {
			res,
			detail: require_shopFloorPlanPure.pickFloorPlanDetail(res)
		};
	});
	return _fetchShopFloorPlanByCodeResponse.apply(this, arguments);
}
function createShopFloorPlanClient(options) {
	var _options$saveFailedMe;
	const { http } = options;
	const saveFailed = (_options$saveFailedMe = options.saveFailedMessage) !== null && _options$saveFailedMe !== void 0 ? _options$saveFailedMe : "Failed to save floor plan";
	function postShopFloorPlan(_x4) {
		return _postShopFloorPlan.apply(this, arguments);
	}
	function _postShopFloorPlan() {
		_postShopFloorPlan = require_asyncToGenerator._asyncToGenerator(function* (body) {
			var _data;
			const res = yield http.post("/shop/schedule/floor-plan", body, {
				fullResult: true,
				isShopApi: true
			});
			if ((res === null || res === void 0 ? void 0 : res.code) === 200 || (res === null || res === void 0 ? void 0 : res.status) === true || (res === null || res === void 0 || (_data = res.data) === null || _data === void 0 ? void 0 : _data.status) === true) {
				const detail = require_shopFloorPlanPure.pickFloorPlanDetail(res);
				if (detail) return detail;
				throw new Error(saveFailed);
			}
			if ((res === null || res === void 0 ? void 0 : res.code) === 5001) {
				const err = new Error(typeof res.message === "string" && res.message.trim() ? res.message : "Floor plan code already exists");
				Object.assign(err, { [require_shopFloorPlanTypes.SHOP_FLOOR_PLAN_DUPLICATE_CODE]: true });
				throw err;
			}
			const msg = (res === null || res === void 0 ? void 0 : res.message) || (typeof (res === null || res === void 0 ? void 0 : res.data) === "object" && (res === null || res === void 0 ? void 0 : res.data) && "message" in res.data && typeof res.data.message === "string" ? res.data.message : null) || saveFailed;
			throw new Error(msg);
		});
		return _postShopFloorPlan.apply(this, arguments);
	}
	function ensureShopFloorPlanByCode(_x5, _x6) {
		return _ensureShopFloorPlanByCode.apply(this, arguments);
	}
	function _ensureShopFloorPlanByCode() {
		_ensureShopFloorPlanByCode = require_asyncToGenerator._asyncToGenerator(function* (code, subscribe) {
			const trimmed = code === null || code === void 0 ? void 0 : code.trim();
			if (!trimmed) return null;
			const _ref = subscribe !== null && subscribe !== void 0 ? subscribe : {}, { buildDefaultPostBody } = _ref, subscribeRest = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
			const postCreateOrRecover = function() {
				var _ref2 = require_asyncToGenerator._asyncToGenerator(function* () {
					if (!buildDefaultPostBody) return null;
					const body = buildDefaultPostBody(trimmed);
					try {
						return yield postShopFloorPlan(body);
					} catch (e) {
						if (require_shopFloorPlanPure.isFloorPlanDuplicateCodeError(e)) {
							const { detail: again } = yield fetchShopFloorPlanByCodeResponse(http, trimmed, subscribeRest);
							return again;
						}
						throw e;
					}
				});
				return function postCreateOrRecover() {
					return _ref2.apply(this, arguments);
				};
			}();
			try {
				const { res, detail } = yield fetchShopFloorPlanByCodeResponse(http, trimmed, subscribeRest);
				if (detail) return detail;
				if (require_shopFloorPlanPure.isShopFloorPlanNotFoundResponse(res)) return yield postCreateOrRecover();
				return null;
			} catch (err) {
				if (require_shopFloorPlanPure.isHttpNotFoundError(err)) return yield postCreateOrRecover();
				throw err;
			}
		});
		return _ensureShopFloorPlanByCode.apply(this, arguments);
	}
	return {
		unsubscribeShopFloorPlanCodeQuery(subscriberId) {
			return require_asyncToGenerator._asyncToGenerator(function* () {
				if (!subscriberId) return;
				yield http.get("/shop/schedule/floor-plan/code/unsubscribe", { subscriberId }, {
					isShopApi: true,
					osServer: true
				});
			})();
		},
		getShopFloorPlanByCode(code, subscribe) {
			return require_asyncToGenerator._asyncToGenerator(function* () {
				const trimmed = code === null || code === void 0 ? void 0 : code.trim();
				if (!trimmed) return null;
				const { detail } = yield fetchShopFloorPlanByCodeResponse(http, trimmed, subscribe);
				return detail;
			})();
		},
		postShopFloorPlan,
		putShopFloorPlan(id, body) {
			return require_asyncToGenerator._asyncToGenerator(function* () {
				var _data2, _res$data;
				const res = yield http.put(`/shop/schedule/floor-plan/${id}`, body, {
					fullResult: true,
					isShopApi: true
				});
				if (!((res === null || res === void 0 ? void 0 : res.code) === 200 || (res === null || res === void 0 ? void 0 : res.status) === true || (res === null || res === void 0 || (_data2 = res.data) === null || _data2 === void 0 ? void 0 : _data2.status) === true)) {
					const msg = (res === null || res === void 0 ? void 0 : res.message) || (typeof (res === null || res === void 0 ? void 0 : res.data) === "object" && (res === null || res === void 0 ? void 0 : res.data) && "message" in res.data && typeof res.data.message === "string" ? res.data.message : null) || saveFailed;
					throw new Error(msg);
				}
				return (_res$data = res === null || res === void 0 ? void 0 : res.data) !== null && _res$data !== void 0 ? _res$data : res;
			})();
		},
		ensureShopFloorPlanByCode
	};
}
//#endregion
exports.createShopFloorPlanClient = createShopFloorPlanClient;
