/**
 * 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 field_grid_layout_exports = {};
__export(field_grid_layout_exports, {
  buildFlowSurfaceAutoFieldGridLayout: () => buildFlowSurfaceAutoFieldGridLayout,
  resolveFlowSurfaceFieldGridFieldInterface: () => resolveFlowSurfaceFieldGridFieldInterface
});
module.exports = __toCommonJS(field_grid_layout_exports);
var import_service_helpers = require("./service-helpers");
const FLOW_SURFACE_FIELD_GRID_BLOCK_TYPES = /* @__PURE__ */ new Set(["createForm", "editForm", "details", "filterForm"]);
const FLOW_SURFACE_WIDE_FIELD_GRID_BLOCK_TYPES = /* @__PURE__ */ new Set(["createForm", "editForm", "details"]);
const FLOW_SURFACE_WIDE_FIELD_INTERFACES = /* @__PURE__ */ new Set(["richtext", "vditor"]);
function normalizeText(value) {
  return String(value || "").trim();
}
function resolveFlowSurfaceFieldGridFieldInterface(input) {
  const fieldPath = (0, import_service_helpers.normalizeFieldPath)(normalizeText(input.fieldPath), normalizeText(input.associationPathName));
  if (!input.collection || !fieldPath) {
    return void 0;
  }
  return (0, import_service_helpers.getFieldInterface)((0, import_service_helpers.resolveFieldFromCollection)(input.collection, fieldPath));
}
function buildCompactFieldGridRow(keys, blockType) {
  const normalizedKeys = keys.filter(Boolean);
  if (!normalizedKeys.length) {
    return [];
  }
  if (blockType === "filterForm") {
    const span = normalizedKeys.length === 1 ? 24 : normalizedKeys.length === 2 ? 12 : 8;
    return normalizedKeys.map((key) => ({ key, span }));
  }
  if (normalizedKeys.length === 1) {
    return [{ key: normalizedKeys[0], span: 24 }];
  }
  return normalizedKeys.map((key) => ({ key, span: 12 }));
}
function isStandaloneFieldGridField(field, blockType) {
  if (normalizeText(field.type) === "divider") {
    return true;
  }
  if (!FLOW_SURFACE_WIDE_FIELD_GRID_BLOCK_TYPES.has(blockType || "")) {
    return false;
  }
  return FLOW_SURFACE_WIDE_FIELD_INTERFACES.has(normalizeText(field.fieldInterface).toLowerCase());
}
function buildFlowSurfaceAutoFieldGridLayout(fields, blockType) {
  if (!FLOW_SURFACE_FIELD_GRID_BLOCK_TYPES.has(blockType || "") || !fields.length) {
    return void 0;
  }
  const rows = [];
  const chunkSize = blockType === "filterForm" ? 3 : 2;
  let pendingKeys = [];
  const flushPending = () => {
    if (!pendingKeys.length) {
      return;
    }
    rows.push(buildCompactFieldGridRow(pendingKeys, blockType));
    pendingKeys = [];
  };
  fields.forEach((field) => {
    const key = normalizeText(field == null ? void 0 : field.key);
    if (!key) {
      return;
    }
    if (isStandaloneFieldGridField(field, blockType)) {
      flushPending();
      rows.push([{ key, span: 24 }]);
      return;
    }
    pendingKeys.push(key);
    if (pendingKeys.length >= chunkSize) {
      flushPending();
    }
  });
  flushPending();
  return rows.length ? { rows } : void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildFlowSurfaceAutoFieldGridLayout,
  resolveFlowSurfaceFieldGridFieldInterface
});
