/**
 * 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 __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var public_compatibility_exports = {};
__export(public_compatibility_exports, {
  FLOW_SURFACE_DEFAULT_SORTING: () => FLOW_SURFACE_DEFAULT_SORTING,
  normalizeFlowSurfaceDefaultSorting: () => normalizeFlowSurfaceDefaultSorting,
  normalizeFlowSurfaceEmptySortingAsDefault: () => normalizeFlowSurfaceEmptySortingAsDefault,
  normalizeFlowSurfacePublicSortingAlias: () => normalizeFlowSurfacePublicSortingAlias
});
module.exports = __toCommonJS(public_compatibility_exports);
var import_lodash = __toESM(require("lodash"));
var import_configure_options = require("./configure-options");
var import_errors = require("./errors");
var import_support_matrix = require("./support-matrix");
const FLOW_SURFACE_DEFAULT_SORTING = [{ field: "createdAt", direction: "desc" }];
function resolveBlockUse(input) {
  var _a;
  const explicitUse = String(input.use || "").trim();
  if (explicitUse) {
    return explicitUse;
  }
  const typeOrUse = String(input.type || "").trim();
  if (!typeOrUse) {
    return void 0;
  }
  if (import_support_matrix.FLOW_SURFACE_BLOCK_SUPPORT_BY_USE.has(typeOrUse)) {
    return typeOrUse;
  }
  return ((_a = import_support_matrix.FLOW_SURFACE_BLOCK_SUPPORT_BY_KEY.get(typeOrUse)) == null ? void 0 : _a.modelUse) || typeOrUse;
}
function blockSupportsSorting(use) {
  return !!use && (0, import_configure_options.getConfigureOptionKeysForUse)(use).includes("sorting");
}
function normalizeSortingDirection(input, context) {
  const normalized = String(input || "").trim().toLowerCase();
  if (!normalized || normalized === "asc" || normalized === "ascend" || normalized === "ascending") {
    return "asc";
  }
  if (normalized === "desc" || normalized === "descend" || normalized === "descending") {
    return "desc";
  }
  (0, import_errors.throwBadRequest)(`${context} must be 'asc' or 'desc'`);
}
function normalizeSortingField(input, context) {
  const field = String(input || "").trim();
  if (!field) {
    (0, import_errors.throwBadRequest)(`${context} must be a non-empty field name`);
  }
  return field;
}
function normalizeSortingEntry(input, context) {
  if (import_lodash.default.isString(input)) {
    const value = input.trim();
    if (!value) {
      (0, import_errors.throwBadRequest)(`${context} must be a non-empty sort field`);
    }
    const descending = value.startsWith("-");
    const field = descending ? value.slice(1).trim() : value;
    return {
      field: normalizeSortingField(field, context),
      direction: descending ? "desc" : "asc"
    };
  }
  if (!import_lodash.default.isPlainObject(input)) {
    (0, import_errors.throwBadRequest)(`${context} must be a string or an object with field and direction`);
  }
  return {
    field: normalizeSortingField(input.field, `${context}.field`),
    direction: normalizeSortingDirection(input.direction, `${context}.direction`)
  };
}
function normalizeSortingArray(input, context) {
  if (!Array.isArray(input)) {
    (0, import_errors.throwBadRequest)(`${context} must be an array`);
  }
  return input.map((item, index) => normalizeSortingEntry(item, `${context}[${index}]`));
}
function normalizeFlowSurfaceDefaultSorting(input, context) {
  if (Array.isArray(input)) {
    return normalizeSortingArray(input, context);
  }
  if (import_lodash.default.isUndefined(input) || import_lodash.default.isNull(input)) {
    return import_lodash.default.cloneDeep(FLOW_SURFACE_DEFAULT_SORTING);
  }
  (0, import_errors.throwBadRequest)(`${context} must be an array`);
}
function normalizeFlowSurfaceEmptySortingAsDefault(input, context) {
  if (Array.isArray(input) && !input.length) {
    return import_lodash.default.cloneDeep(FLOW_SURFACE_DEFAULT_SORTING);
  }
  return normalizeFlowSurfaceDefaultSorting(input, context);
}
function normalizeFlowSurfacePublicSortingAlias(input) {
  if (import_lodash.default.isUndefined(input.settings)) {
    return input.settings;
  }
  if (!import_lodash.default.isPlainObject(input.settings)) {
    (0, import_errors.throwBadRequest)(`${input.context} must be an object`);
  }
  if (!Object.prototype.hasOwnProperty.call(input.settings, "sort")) {
    return input.settings;
  }
  const use = resolveBlockUse(input);
  if (!blockSupportsSorting(use)) {
    return input.settings;
  }
  const nextSettings = import_lodash.default.cloneDeep(input.settings);
  const sortingFromSort = normalizeFlowSurfaceDefaultSorting(nextSettings.sort, `${input.context}.sort`);
  if (Object.prototype.hasOwnProperty.call(nextSettings, "sorting")) {
    const canonicalSorting = normalizeFlowSurfaceDefaultSorting(nextSettings.sorting, `${input.context}.sorting`);
    if (!import_lodash.default.isEqual(sortingFromSort, canonicalSorting)) {
      (0, import_errors.throwBadRequest)(`${input.context}.sort conflicts with ${input.context}.sorting; use canonical sorting`);
    }
    nextSettings.sorting = canonicalSorting;
  } else {
    nextSettings.sorting = sortingFromSort;
  }
  delete nextSettings.sort;
  return nextSettings;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FLOW_SURFACE_DEFAULT_SORTING,
  normalizeFlowSurfaceDefaultSorting,
  normalizeFlowSurfaceEmptySortingAsDefault,
  normalizeFlowSurfacePublicSortingAlias
});
