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);

// src/components/systemSettings/registry/components/DataSourceTreeSelect/serve.ts
var serve_exports = {};
__export(serve_exports, {
  getAllDataSources: () => getAllDataSources,
  getNocobaseDataSource: () => getNocobaseDataSource,
  getNocobaseDataSourceOptions: () => getNocobaseDataSourceOptions,
  getNocobaseMainDataSource: () => getNocobaseMainDataSource,
  getNocobaseMenuDataSource: () => getNocobaseMenuDataSource,
  mergeDataSources: () => mergeDataSources
});
module.exports = __toCommonJS(serve_exports);
var import_utils = require("../../../../../utils");
var get = async (url, params = {}, config = {}) => {
  try {
    const req = import_utils.request.getRequest();
    const response = await req.get(url, params, config);
    return response;
  } catch (error) {
    console.error("GET request failed:", error);
    return null;
  }
};
var post = async (url, data = {}, config = {}) => {
  try {
    const req = import_utils.request.getRequest();
    const response = await req.post(url, data, config);
    return response;
  } catch (error) {
    console.error("POST request failed:", error);
    return null;
  }
};
var getNocobaseDataSource = async () => {
  try {
    const res = await post(
      "/dataSources:listEnabled?paginate=false&appends%5B%5D=collections",
      {},
      {
        isNocobase: true
      }
    );
    const data = Array.isArray(res) ? res : (res == null ? void 0 : res.data) || [];
    return data;
  } catch (error) {
    console.error("Failed to fetch nocobase data sources:", error);
    return [];
  }
};
var getNocobaseMainDataSource = async () => {
  try {
    const res = await post(
      "/collections:list?paginate=false&appends%5B%5D=fields&sort%5B%5D=sort&filter=%7B%7D",
      {},
      {
        isNocobase: true
      }
    );
    const data = Array.isArray(res) ? res : (res == null ? void 0 : res.data) || [];
    return data;
  } catch (error) {
    console.error("Failed to fetch nocobase main data source:", error);
    return [];
  }
};
var getNocobaseDataSourceOptions = async () => {
  try {
    const res = await post(
      "/dataSources:list?appends=collections",
      {},
      {
        isNocobase: true
      }
    );
    const data = Array.isArray(res) ? res : (res == null ? void 0 : res.data) || [];
    return data;
  } catch (error) {
    console.error("Failed to fetch nocobase data source options:", error);
    return [];
  }
};
var mergeDataSources = (main, other, options) => {
  const safeMain = Array.isArray(main) ? main : [];
  const safeOther = Array.isArray(other) ? other : [];
  const safeOptions = Array.isArray(options) ? options : [];
  const optionsMap = safeOptions.reduce((acc, item) => {
    if (item && item.key) {
      acc[item.key] = item;
    }
    return acc;
  }, {});
  const otherDataSource = safeOther.map((item) => ({
    ...item,
    options: optionsMap[item.key]
  }));
  return [
    {
      displayName: "主数据源",
      key: "main",
      type: "main",
      collections: safeMain
    },
    ...otherDataSource
  ];
};
var getAllDataSources = async () => {
  try {
    const [other, main, options] = await Promise.all([
      getNocobaseDataSource(),
      getNocobaseMainDataSource(),
      getNocobaseDataSourceOptions()
    ]);
    console.log("Data sources fetched:", {
      other: Array.isArray(other) ? `${other.length} items` : typeof other,
      main: Array.isArray(main) ? `${main.length} items` : typeof main,
      options: Array.isArray(options) ? `${options.length} items` : typeof options
    });
    const merged = mergeDataSources(main, other, options);
    console.log("Merged data sources:", merged.length, "items");
    return merged;
  } catch (error) {
    console.error("Failed to fetch all data sources:", error);
    return [];
  }
};
var getNocobaseMenuDataSource = async () => {
  try {
    const res = await get(
      "/navigation_menu:list",
      {
        page: 1,
        pageSize: 100,
        filter: {}
      },
      {
        isNocobase: true,
        headers: {
          "x-data-source": "pisell2"
        }
      }
    );
    const data = Array.isArray(res) ? res : (res == null ? void 0 : res.data) || [];
    console.log("Menu data source fetched:", {
      type: typeof res,
      isArray: Array.isArray(res),
      dataLength: data.length
    });
    return {
      title: "菜单",
      value: "menu",
      selectable: false,
      children: data.map((item) => {
        var _a;
        return {
          title: ((_a = item.name) == null ? void 0 : _a["original"]) || item.name || "Untitled",
          value: `{{global.value.menu.${item.code}}}`
        };
      })
    };
  } catch (error) {
    console.error("Failed to fetch menu data source:", error);
    return {
      title: "菜单",
      value: "menu",
      selectable: false,
      children: []
    };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getAllDataSources,
  getNocobaseDataSource,
  getNocobaseDataSourceOptions,
  getNocobaseMainDataSource,
  getNocobaseMenuDataSource,
  mergeDataSources
});
