/**
 * 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 selects_exports = {};
__export(selects_exports, {
  adjustSelectsForCollection: () => adjustSelectsForCollection
});
module.exports = __toCommonJS(selects_exports);
function adjustSelectsForCollection(koaCtx, dataSourceKey, collection, fields, appends) {
  var _a, _b, _c, _d;
  const ds = koaCtx.app.dataSourceManager.get(dataSourceKey || "main");
  const cm = ds.collectionManager;
  const coll = (_b = (_a = cm == null ? void 0 : cm.db) == null ? void 0 : _a.getCollection) == null ? void 0 : _b.call(_a, collection);
  const assocKeys = Object.keys(((_c = coll == null ? void 0 : coll.model) == null ? void 0 : _c.associations) || {});
  const rawAttrs = ((_d = coll == null ? void 0 : coll.model) == null ? void 0 : _d.rawAttributes) || {};
  const toCamel = (s) => s.replace(/_([a-zA-Z0-9])/g, (_m, c) => String(c).toUpperCase());
  const toSnake = (s) => s.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
  const assocMap = /* @__PURE__ */ new Map();
  for (const k of assocKeys) {
    assocMap.set(k, k);
    assocMap.set(toSnake(k), k);
    assocMap.set(toCamel(k), k);
  }
  const outFields = [];
  const outAppends = new Set(appends || []);
  for (const f of fields || []) {
    const segs = String(f).split(".").filter(Boolean);
    if (!segs.length) continue;
    const first = segs[0];
    const assocCanonical = assocMap.get(first) || assocMap.get(toCamel(first)) || assocMap.get(toSnake(first));
    if (assocCanonical) {
      outAppends.add(assocCanonical);
      if (segs.length === 1) {
        continue;
      }
      outFields.push([assocCanonical, ...segs.slice(1)].join("."));
      continue;
    }
    if (rawAttrs[first]) {
      outFields.push(f);
    } else if (rawAttrs[toSnake(first)]) {
      outFields.push([toSnake(first), ...segs.slice(1)].join("."));
    } else if (rawAttrs[toCamel(first)]) {
      outFields.push([toCamel(first), ...segs.slice(1)].join("."));
    } else {
      continue;
    }
  }
  return {
    fields: outFields.length ? outFields : void 0,
    appends: outAppends.size ? Array.from(outAppends) : void 0
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  adjustSelectsForCollection
});
