/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

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);
var compose_compiler_exports = {};
__export(compose_compiler_exports, {
  compileComposeExecutionPlan: () => compileComposeExecutionPlan,
  resolveComposeFieldContainerSource: () => resolveComposeFieldContainerSource,
  resolveComposeTargetKey: () => resolveComposeTargetKey
});
module.exports = __toCommonJS(compose_compiler_exports);
var import_errors = require("./errors");
var import_defaults = require("./blueprint/defaults");
const LIST_LIKE_COMPOSE_BLOCK_TYPES = /* @__PURE__ */ new Set(["list", "gridCard", "kanban"]);
function compileComposeExecutionPlan(input) {
  const blocks = compileComposeBlockTasks(input.gridUid, input.normalizedBlocks);
  const fields = compileComposeFieldTasks(input.normalizedBlocks);
  const actions = compileComposeActionTasks(input.normalizedBlocks);
  const recordActions = compileComposeRecordActionTasks(input.normalizedBlocks);
  return {
    gridUid: input.gridUid,
    mode: input.mode,
    existingItemUids: Array.from(new Set((input.existingItemUids || []).filter(Boolean))),
    blocks,
    fields,
    actions,
    recordActions,
    layoutPlan: buildComposeLayoutPlan({
      mode: input.mode,
      layout: input.layout,
      hasBlocks: blocks.length > 0
    })
  };
}
function compileComposeBlockTasks(gridUid, normalizedBlocks) {
  return normalizedBlocks.map((blockSpec) => ({
    key: blockSpec.key,
    spec: blockSpec,
    payload: buildComposeBlockCreatePayload(gridUid, blockSpec)
  }));
}
function compileComposeFieldTasks(normalizedBlocks) {
  return normalizedBlocks.flatMap(
    (blockSpec) => blockSpec.fields.map((fieldSpec) => ({
      blockKey: blockSpec.key,
      spec: fieldSpec,
      containerSource: resolveComposeFieldContainerSource(blockSpec),
      payload: buildComposeFieldCreatePayload(fieldSpec)
    }))
  );
}
function compileComposeActionTasks(normalizedBlocks) {
  return normalizedBlocks.flatMap(
    (blockSpec) => blockSpec.actions.map((actionSpec) => ({
      blockKey: blockSpec.key,
      spec: actionSpec,
      payload: buildComposeActionCreatePayload(actionSpec)
    }))
  );
}
function compileComposeRecordActionTasks(normalizedBlocks) {
  return normalizedBlocks.flatMap(
    (blockSpec) => blockSpec.recordActions.map((actionSpec) => ({
      blockKey: blockSpec.key,
      spec: actionSpec,
      payload: buildComposeRecordActionCreatePayload(actionSpec)
    }))
  );
}
function resolveComposeFieldContainerSource(blockSpec) {
  return LIST_LIKE_COMPOSE_BLOCK_TYPES.has(blockSpec.type || "") ? "item" : "block";
}
function resolveComposeTargetKey(targetKey, keyMap, kind) {
  var _a, _b;
  const key = String(targetKey || "").trim();
  if (!key) {
    (0, import_errors.throwBadRequest)(`flowSurfaces compose ${kind} target key cannot be empty`);
  }
  if (!((_a = keyMap[key]) == null ? void 0 : _a.uid)) {
    (0, import_errors.throwBadRequest)(`flowSurfaces compose ${kind} target '${key}' was not created in the current compose call`);
  }
  return (_b = keyMap[key]) == null ? void 0 : _b.uid;
}
function buildComposeBlockCreatePayload(gridUid, blockSpec) {
  var _a, _b;
  return {
    target: {
      uid: gridUid
    },
    ...blockSpec.key ? { key: blockSpec.key } : {},
    ...blockSpec.type ? { type: blockSpec.type } : {},
    ...((_a = blockSpec.resource) == null ? void 0 : _a.kind) === "semantic" ? { resource: blockSpec.resource.value } : {},
    ...((_b = blockSpec.resource) == null ? void 0 : _b.kind) === "raw" ? { resourceInit: blockSpec.resource.value } : {},
    ...blockSpec.template ? { template: blockSpec.template } : {}
  };
}
function buildComposeFieldCreatePayload(fieldSpec) {
  return {
    ...fieldSpec.key ? { key: fieldSpec.key } : {},
    ...fieldSpec.fieldPath ? { fieldPath: fieldSpec.fieldPath } : {},
    ...fieldSpec.associationPathName ? { associationPathName: fieldSpec.associationPathName } : {},
    ...fieldSpec.renderer ? { renderer: fieldSpec.renderer } : {},
    ...fieldSpec.type ? { type: fieldSpec.type } : {},
    ...fieldSpec.fieldType ? { fieldType: fieldSpec.fieldType } : {},
    ...typeof fieldSpec.fields !== "undefined" ? { fields: fieldSpec.fields } : {},
    ...fieldSpec.titleField ? { titleField: fieldSpec.titleField } : {},
    ...fieldSpec.openMode ? { openMode: fieldSpec.openMode } : {},
    ...fieldSpec.popupSize ? { popupSize: fieldSpec.popupSize } : {},
    ...typeof fieldSpec.pageSize !== "undefined" ? { pageSize: fieldSpec.pageSize } : {},
    ...typeof fieldSpec.showIndex !== "undefined" ? { showIndex: fieldSpec.showIndex } : {},
    ...fieldSpec.defaultTargetUid ? { defaultTargetUid: fieldSpec.defaultTargetUid } : {},
    ...fieldSpec.targetBlockUid ? { targetBlockUid: fieldSpec.targetBlockUid } : {},
    ...fieldSpec.popup ? { popup: fieldSpec.popup } : {},
    ...fieldSpec.__autoPopupForRelationField ? { __autoPopupForRelationField: true } : {},
    ...fieldSpec[import_defaults.FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY] ? {
      [import_defaults.FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY]: fieldSpec[import_defaults.FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY]
    } : {}
  };
}
function buildComposeActionCreatePayload(actionSpec) {
  return {
    ...actionSpec.key ? { key: actionSpec.key } : {},
    type: actionSpec.type
  };
}
function buildComposeRecordActionCreatePayload(actionSpec) {
  return {
    ...actionSpec.key ? { key: actionSpec.key } : {},
    type: actionSpec.type
  };
}
function buildComposeLayoutPlan(input) {
  var _a;
  if (((_a = input.layout) == null ? void 0 : _a.rows) || input.mode === "replace") {
    return {
      kind: "explicit",
      layout: input.layout
    };
  }
  if (input.mode === "append" && input.hasBlocks) {
    return {
      kind: "append"
    };
  }
  return {
    kind: "none"
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  compileComposeExecutionPlan,
  resolveComposeFieldContainerSource,
  resolveComposeTargetKey
});
