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

// src/components/dataSourceComponents/dataSourceForm/serve.ts
var serve_exports = {};
__export(serve_exports, {
  createNocobaseData: () => createNocobaseData,
  customApi: () => customApi,
  deleteNocobaseData: () => deleteNocobaseData,
  getDeviceSettingMetaData: () => getDeviceSettingMetaData,
  getFormSettingMetaData: () => getFormSettingMetaData,
  getNocobaseData: () => getNocobaseData,
  getNocobaseDataList: () => getNocobaseDataList,
  getNocobaseDataSource: () => getNocobaseDataSource,
  getNocobaseDataSourceOptions: () => getNocobaseDataSourceOptions,
  getNocobaseMainDataSource: () => getNocobaseMainDataSource,
  updateDeviceSetting: () => updateDeviceSetting,
  updateNocobaseData: () => updateNocobaseData,
  updateTenantSetting: () => updateTenantSetting
});
module.exports = __toCommonJS(serve_exports);
var import_utils = require("../../table/utils");
var import_locales = require("../../../locales");
var import_pisellToast = __toESM(require("../../pisellToast"));
var getNocobaseDataSource = async () => {
  return import_utils.request.getRequest().post(
    "/dataSources:listEnabled?paginate=false&appends%5B%5D=collections",
    {},
    {
      isNocobase: true
    }
  );
};
var getNocobaseMainDataSource = async () => {
  return import_utils.request.getRequest().post(
    "/collections:list?paginate=false&appends%5B%5D=fields&sort%5B%5D=sort&filter=%7B%7D",
    {},
    {
      isNocobase: true
    }
  );
};
var getNocobaseDataSourceOptions = async () => {
  return import_utils.request.getRequest().post(
    "/dataSources:list?appends=collections",
    {},
    {
      isNocobase: true
    }
  );
};
var createNocobaseData = async (params) => {
  const { key, data, headers } = params;
  return import_utils.request.getRequest().post(`/${key}:create`, data, {
    isNocobase: true,
    headers
  });
};
var formatQueryObjString = (query = {}) => {
  return Object.entries(query || {}).map(
    ([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
  ).join("&");
};
var getNocobaseData = async (params) => {
  const { key, query, headers } = params;
  return import_utils.request.getRequest().get(
    `/${key}:get?${formatQueryObjString(query)}`,
    {},
    {
      isNocobase: true,
      headers
    }
  );
};
var methodMap = {
  GET: "get",
  POST: "post",
  PUT: "put",
  DELETE: "remove"
};
var formatHeaders = (headers) => {
  return headers == null ? void 0 : headers.reduce((acc, curr) => {
    acc[curr.name] = curr.value;
    return acc;
  }, {});
};
var formatQueryString = (params = []) => params.map(
  ({ name, value }) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
).join("&");
var formatRequestUrl = (url, baseUrl, queryString) => {
  const baseRequestUrl = baseUrl ? `${baseUrl}${url}` : url;
  return queryString ? `${baseRequestUrl}${baseRequestUrl.includes("?") ? "&" : "?"}${queryString}` : baseRequestUrl;
};
var customApi = async (url, data, options) => {
  const {
    method,
    baseUrl,
    headers = [],
    params = [],
    body = {},
    dataSourceKey
  } = options;
  const requestHeaders = formatHeaders(headers);
  const queryString = formatQueryString(params);
  const requestUrl = formatRequestUrl(url, baseUrl, queryString);
  const requestData = method === "GET" ? data : { ...body, ...data };
  const requestMethod = methodMap[method];
  return import_utils.request.getRequest()[requestMethod](requestUrl, requestData, {
    headers: requestHeaders,
    isCustomApi: true,
    isShopApi: dataSourceKey === "mypisell-api"
  });
};
var updateNocobaseData = async (params) => {
  const { key, data, headers, query } = params;
  return import_utils.request.getRequest().post(`/${key}:update?${formatQueryObjString(query)}`, data, {
    isNocobase: true,
    headers
  });
};
var getNocobaseDataList = async (allParams) => {
  const { key, params, headers } = allParams;
  return import_utils.request.getRequest().get(`/${key}:list`, params, {
    isNocobase: true,
    fullResult: true,
    headers
  });
};
var deleteNocobaseData = async (key, id, headers) => {
  return import_utils.request.getRequest().post(
    `/${key}:destroy?filterByTk=${id}`,
    {},
    {
      isNocobase: true,
      headers
    }
  );
};
var getFormSettingMetaData = async (params) => {
  console.log("getFormSettingMetaData", params);
  const { sectionCodes, scope, target } = params;
  const url = `/tenant/setting`;
  if (!scope) {
    (0, import_pisellToast.default)({
      content: (0, import_locales.getText)("pisell-data-source-form-scope-message")
    });
    return {};
  }
  const payload = {
    scope,
    section_code: sectionCodes,
    target
  };
  return import_utils.request.getRequest().post(url, payload, {
    headers: {
      "Content-Type": "application/json"
    }
  });
};
var updateTenantSetting = async (params) => {
  console.log("updateTenantSetting", params);
  const { scope, target, data } = params;
  const requestBody = {
    scope,
    target,
    data
  };
  if (!scope) {
    (0, import_pisellToast.default)({
      content: (0, import_locales.getText)("pisell-data-source-form-scope-message")
    });
    return {};
  }
  const url = "/tenant/setting";
  return import_utils.request.getRequest().put(url, requestBody, {
    headers: {
      "Content-Type": "application/json"
    }
  });
};
var buildDeviceSettingUrl = (scene = "template", id) => {
  switch (scene) {
    case "profile":
      return `/tenant/device/profile/${id}/setting`;
    case "instance":
      return `/tenant/device/${id}/setting`;
    case "template":
    default:
      return `/admin/device/profile/template/${id}/setting`;
  }
};
var getDeviceSettingMetaData = async (params) => {
  const { scene, sceneRequestId, items } = params;
  console.log("getDeviceSettingMetaData", params);
  if (!sceneRequestId) {
    (0, import_pisellToast.default)({
      content: (0, import_locales.getText)("pisell-data-source-form-scene-request-id-message")
    });
    return {};
  }
  const url = buildDeviceSettingUrl(scene, sceneRequestId);
  return import_utils.request.getRequest().post(
    url,
    {
      items
    },
    {
      headers: {
        "Content-Type": "application/json"
      },
      isPisell2: true
    }
  );
};
var updateDeviceSetting = async (params) => {
  const { scene, sceneRequestId, data } = params;
  console.log("updateDeviceSetting", params);
  if (!sceneRequestId) {
    (0, import_pisellToast.default)({
      content: (0, import_locales.getText)("pisell-data-source-form-scene-request-id-message")
    });
    return {};
  }
  const url = buildDeviceSettingUrl(scene, sceneRequestId);
  return import_utils.request.getRequest().put(
    url,
    {
      items: data
    },
    {
      headers: {
        "Content-Type": "application/json"
      },
      isPisell2: true
    }
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createNocobaseData,
  customApi,
  deleteNocobaseData,
  getDeviceSettingMetaData,
  getFormSettingMetaData,
  getNocobaseData,
  getNocobaseDataList,
  getNocobaseDataSource,
  getNocobaseDataSourceOptions,
  getNocobaseMainDataSource,
  updateDeviceSetting,
  updateNocobaseData,
  updateTenantSetting
});
