/**
 * 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 utils_exports = {};
__export(utils_exports, {
  MAX_QUERY_LIMIT: () => MAX_QUERY_LIMIT,
  MAX_STRING_LENGTH: () => MAX_STRING_LENGTH,
  buildPagedToolResult: () => buildPagedToolResult,
  normalizeLimitOffset: () => normalizeLimitOffset,
  truncateLongStrings: () => truncateLongStrings
});
module.exports = __toCommonJS(utils_exports);
const MAX_STRING_LENGTH = 500;
const MAX_QUERY_LIMIT = 100;
function normalizeLimitOffset(args, options) {
  const defaultLimit = (options == null ? void 0 : options.defaultLimit) ?? 50;
  const maxLimit = (options == null ? void 0 : options.maxLimit) ?? MAX_QUERY_LIMIT;
  const rawLimit = typeof args.limit === "number" && Number.isFinite(args.limit) ? args.limit : void 0;
  const rawOffset = typeof args.offset === "number" && Number.isFinite(args.offset) ? args.offset : void 0;
  const limit = Math.min(Math.max(rawLimit ?? defaultLimit, 1), maxLimit);
  const offset = Math.max(rawOffset ?? 0, 0);
  return { limit, offset };
}
function buildPagedToolResult(params) {
  const returned = params.records.length;
  const nextOffset = params.offset + returned;
  const hasMore = params.total > nextOffset;
  return {
    total: params.total,
    offset: params.offset,
    limit: params.limit,
    returned,
    hasMore,
    nextOffset,
    records: params.records
  };
}
function truncateLongStrings(obj, maxLen = MAX_STRING_LENGTH) {
  if (obj === null || obj === void 0) {
    return obj;
  }
  if (typeof obj === "string") {
    return obj.length > maxLen ? obj.slice(0, maxLen) + "...[truncated]" : obj;
  }
  if (Array.isArray(obj)) {
    return obj.map((item) => truncateLongStrings(item, maxLen));
  }
  if (typeof obj === "object") {
    const result = {};
    for (const key in obj) {
      result[key] = truncateLongStrings(obj[key], maxLen);
    }
    return result;
  }
  return obj;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  MAX_QUERY_LIMIT,
  MAX_STRING_LENGTH,
  buildPagedToolResult,
  normalizeLimitOffset,
  truncateLongStrings
});
