function parseResourceId(recordId) {
  if (recordId == null || recordId === '') return null;
  var n = Number(recordId);
  return Number.isFinite(n) && n > 0 ? Math.trunc(n) : null;
}
function isCircleShape(el) {
  return el.shape === 'circle';
}

/**
 * 将场景图元转为后端 layout 项（多画布时通常摊平到同一 layout）。
 */
export function sceneElementsToShopLayout(sceneElements) {
  return sceneElements.map(function (el) {
    var _el$width, _el$height, _el$extensions, _el$extensions2, _el$name, _el$fillColor, _el$strokeColor, _el$dataBinding;
    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;
    var circle = isCircleShape(el);
    var rotation = typeof ((_el$extensions = el.extensions) === null || _el$extensions === void 0 ? void 0 : _el$extensions.rotation) === 'number' ? el.extensions.rotation : Number((_el$extensions2 = el.extensions) === null || _el$extensions2 === void 0 ? void 0 : _el$extensions2.rotation) || 0;
    var base = {
      id: el.instanceId,
      type: circle ? 'circle' : 'rect',
      label: ((_el$name = el.name) === null || _el$name === void 0 ? void 0 : _el$name.trim()) || '',
      x: el.x,
      y: el.y,
      rotation: rotation,
      color: (_el$fillColor = el.fillColor) !== null && _el$fillColor !== void 0 ? _el$fillColor : '#1a2332',
      border_color: (_el$strokeColor = el.strokeColor) !== null && _el$strokeColor !== void 0 ? _el$strokeColor : '#ffffff',
      resource_id: parseResourceId((_el$dataBinding = el.dataBinding) === null || _el$dataBinding === void 0 ? void 0 : _el$dataBinding.recordId)
    };
    if (circle) {
      base.radius = Math.max(1, Math.round(Math.min(w, h) / 2));
    } else {
      base.width = w;
      base.height = h;
    }
    return base;
  });
}
/**
 * 将扁平 layout 数组转为 `sceneElements`；图元种类由调用方传入（预约场景可传 boundTable / boundRoundTable 等）。
 */
export function shopLayoutToSceneElements(layout, options) {
  var canvasId = options.canvasId,
    dataSourceKey = options.dataSourceKey,
    rectElementKind = options.rectElementKind,
    circleElementKind = options.circleElementKind;
  return layout.map(function (item) {
    var _item$radius, _item$width, _item$height, _item$label;
    var circle = item.type === 'circle';
    var r = Math.max(1, Math.round((_item$radius = item.radius) !== null && _item$radius !== void 0 ? _item$radius : 40));
    var w = circle ? r * 2 : (_item$width = item.width) !== null && _item$width !== void 0 ? _item$width : 100;
    var h = circle ? r * 2 : (_item$height = item.height) !== null && _item$height !== void 0 ? _item$height : 100;
    var rotation = typeof item.rotation === 'number' ? item.rotation : 0;
    var el = {
      instanceId: String(item.id),
      canvasId: canvasId,
      x: item.x,
      y: item.y,
      width: w,
      height: h,
      name: ((_item$label = item.label) === null || _item$label === void 0 ? void 0 : _item$label.trim()) || undefined,
      zIndex: 0,
      elementKind: circle ? circleElementKind : rectElementKind,
      shape: circle ? 'circle' : undefined,
      fillColor: item.color,
      strokeColor: item.border_color,
      extensions: rotation !== 0 ? {
        rotation: rotation
      } : undefined
    };
    if (item.resource_id != null && Number.isFinite(Number(item.resource_id))) {
      el.dataBinding = {
        dataSourceKey: dataSourceKey,
        recordId: String(Math.trunc(Number(item.resource_id)))
      };
    }
    return el;
  });
}