/**
 * 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 records_exports = {};
__export(records_exports, {
  fetchRecordOrRecordsJson: () => fetchRecordOrRecordsJson,
  getExtraKeyFieldsForSelect: () => getExtraKeyFieldsForSelect,
  mergeFieldsWithExtras: () => mergeFieldsWithExtras
});
module.exports = __toCommonJS(records_exports);
function uniqStrings(list) {
  return Array.from(new Set(list));
}
function isPrimitiveTkArray(arr) {
  return arr.every((v) => typeof v === "string" || typeof v === "number");
}
function getOrderKey(options) {
  if (typeof options.filterTargetKey === "string") return options.filterTargetKey;
  if (options.pkIsValid && options.pkAttr) return options.pkAttr;
  return void 0;
}
function getExtraKeyFieldsForSelect(filterByTk, options) {
  const extra = [];
  if (options.pkIsValid && options.pkAttr) {
    extra.push(options.pkAttr);
  }
  if (Array.isArray(filterByTk) && filterByTk.length > 0) {
    const tkKeys = typeof options.filterTargetKey === "string" ? [options.filterTargetKey] : Array.isArray(options.filterTargetKey) ? options.filterTargetKey : [];
    if (options.rawAttributes) {
      for (const k of tkKeys) {
        if (k && Object.prototype.hasOwnProperty.call(options.rawAttributes, k)) {
          extra.push(k);
        }
      }
    }
  }
  return uniqStrings(extra);
}
function mergeFieldsWithExtras(fields, extras = []) {
  if (!Array.isArray(fields) || fields.length === 0 || extras.length === 0) return fields;
  return uniqStrings([...fields, ...extras]);
}
function toJsonRecord(record) {
  if (!record || typeof record !== "object") return record;
  const toJSON = record.toJSON;
  return typeof toJSON === "function" ? toJSON.call(record) : record;
}
function toJsonArray(rows) {
  if (!Array.isArray(rows)) return [];
  return rows.map(toJsonRecord);
}
function reorderRecordsByFilterByTk(records, filterByTk, options) {
  if (!Array.isArray(filterByTk) || filterByTk.length === 0) return records;
  if (!isPrimitiveTkArray(filterByTk)) return records;
  const orderKey = getOrderKey(options);
  if (!orderKey) return records;
  const map = /* @__PURE__ */ new Map();
  for (const rec of records || []) {
    const k = rec == null ? void 0 : rec[orderKey];
    if (typeof k !== "undefined" && k !== null) {
      map.set(String(k), rec);
    }
  }
  const ordered = [];
  const used = /* @__PURE__ */ new Set();
  for (const tk of filterByTk) {
    const k = String(tk);
    const rec = map.get(k);
    if (typeof rec !== "undefined") {
      ordered.push(rec);
      used.add(k);
    }
  }
  for (const rec of records || []) {
    const k = rec == null ? void 0 : rec[orderKey];
    const ks = typeof k === "undefined" || k === null ? void 0 : String(k);
    if (!ks || used.has(ks)) continue;
    ordered.push(rec);
  }
  return ordered;
}
async function fetchRecordOrRecordsJson(repo, params) {
  const { filterByTk, preferFullRecord, fields, appends } = params;
  if (Array.isArray(filterByTk)) {
    if (filterByTk.length === 0) return [];
    const rows = await repo.find(
      preferFullRecord ? { filterByTk } : { filterByTk, fields, appends }
    );
    const jsonArr = toJsonArray(rows);
    return reorderRecordsByFilterByTk(jsonArr, filterByTk, params);
  }
  const rec = await repo.findOne(
    preferFullRecord ? { filterByTk } : { filterByTk, fields, appends }
  );
  return rec ? toJsonRecord(rec) : void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  fetchRecordOrRecordsJson,
  getExtraKeyFieldsForSelect,
  mergeFieldsWithExtras
});
