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; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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); }
import { SHOP_FLOOR_PLAN_DUPLICATE_CODE } from "./shopFloorPlanTypes";
export function pickFloorPlanDetail(res) {
  var r = res;
  if (!r || _typeof(r) !== 'object') return null;
  var normalize = function normalize(node) {
    if (!node || _typeof(node) !== 'object' || Array.isArray(node)) return null;
    var d = node;
    var idRaw = d.id;
    if (typeof idRaw !== 'number' || !Number.isFinite(idRaw)) return null;
    var hasLayout = d.layout !== undefined && d.layout !== null;
    var hasLayouts = d.layouts !== undefined && d.layouts !== null && (_typeof(d.layouts) === 'object' || typeof d.layouts === 'string');
    if (!hasLayout && !hasLayouts) return null;
    var layout = hasLayout ? d.layout : d.layouts;
    return _objectSpread(_objectSpread({}, d), {}, {
      layout: layout
    });
  };
  var top = r.data;
  var fromTop = normalize(top);
  if (fromTop) return fromTop;
  var nested = top && _typeof(top) === 'object' && 'data' in top ? top.data : null;
  var fromNested = normalize(nested);
  if (fromNested) return fromNested;
  return normalize(r);
}
export function isShopFloorPlanNotFoundResponse(res) {
  if (res == null || _typeof(res) !== 'object') return false;
  var r = res;
  var code = r.code;
  if (code === 404 || code === '404') return true;
  if (r.status === false && (code === 404 || code === '404')) return true;
  var data = r.data;
  if (data && _typeof(data) === 'object' && !Array.isArray(data)) {
    var d = data;
    if (d.code === 404 || d.code === '404') return true;
  }
  return false;
}
export function isHttpNotFoundError(err) {
  var _e$response, _e$response2;
  var 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;
  var 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)) {
    var c = data.code;
    if (c === 404 || c === '404') return true;
  }
  return false;
}
export function isFloorPlanDuplicateCodeError(err) {
  return Boolean(err && _typeof(err) === 'object' && err[SHOP_FLOOR_PLAN_DUPLICATE_CODE] === true);
}
export function parseLayoutFieldToViewConfigPatch(raw) {
  var 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;
  var o = obj;
  if (!Array.isArray(o.sceneElements)) return null;
  var 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;
}
export 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
  };
}
export function mergeFloorPlanViewRemote(base, remote) {
  var _remote$canvases, _remote$elementKinds, _remote$elementKindCa, _remote$sceneElements, _remote$edges, _remote$datasourceCom;
  return _objectSpread(_objectSpread(_objectSpread({}, base), remote), {}, {
    canvasUi: _objectSpread(_objectSpread({}, 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
  });
}
export function viewConfigToLayoutPayload(config) {
  return JSON.parse(JSON.stringify(config));
}
export function inferCanvasSizeFromScene(sceneElements) {
  var fallbackWidth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1920;
  var fallbackHeight = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1080;
  var padding = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 80;
  if (!sceneElements.length) {
    return {
      w: fallbackWidth,
      h: fallbackHeight
    };
  }
  var maxR = 0;
  var maxB = 0;
  var _iterator = _createForOfIteratorHelper(sceneElements),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var _el$width, _el$height;
      var el = _step.value;
      var w = (_el$width = el.width) !== null && _el$width !== void 0 ? _el$width : 100;
      var 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);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return {
    w: Math.max(fallbackWidth, Math.ceil(maxR + padding)),
    h: Math.max(fallbackHeight, Math.ceil(maxB + padding))
  };
}
export function buildFloorPlanPutBody(config, options) {
  var _config$sceneElements, _options$canvasWidth, _options$canvasHeight;
  var scene = (_config$sceneElements = config.sceneElements) !== null && _config$sceneElements !== void 0 ? _config$sceneElements : [];
  var inferred = inferCanvasSizeFromScene(scene);
  return _objectSpread(_objectSpread(_objectSpread(_objectSpread({
    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 !== undefined ? {
    code: options.code
  } : {}), options.name !== undefined ? {
    name: options.name
  } : {}), options.sort !== undefined ? {
    sort: options.sort
  } : {}), options.status !== undefined ? {
    status: options.status
  } : {});
}