/**
 * 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 request_config_exports = {};
__export(request_config_exports, {
  getRunJsActionParamsDataSourcePropertyLookup: () => getRunJsActionParamsDataSourcePropertyLookup,
  getRunJsApiResourceCallDataSourceKey: () => getRunJsApiResourceCallDataSourceKey,
  getRunJsStaticDataSourceKeyFromActionParams: () => getRunJsStaticDataSourceKeyFromActionParams,
  getRunJsStaticDataSourceKeyFromHeaders: () => getRunJsStaticDataSourceKeyFromHeaders,
  getRunJsStaticRequestConfigObjectFromAst: () => getRunJsStaticRequestConfigObjectFromAst,
  getRunJsStaticRequestDataSourceKeyFromAst: () => getRunJsStaticRequestDataSourceKeyFromAst,
  hasRunJsInactiveStaticDataSourceParamBinding: () => hasRunJsInactiveStaticDataSourceParamBinding
});
module.exports = __toCommonJS(request_config_exports);
var import_surface = require("../runtime/surface");
var import_bindings = require("./bindings");
var import_static_values = require("./static-values");
function getRunJsStaticRequestConfigObjectFromAst(node, identifierBindings, staticFilterValueBindings) {
  var _a, _b;
  return (0, import_static_values.resolveRunJsStaticObjectExpression)((_a = node.arguments) == null ? void 0 : _a[0], identifierBindings, staticFilterValueBindings) || (0, import_static_values.resolveRunJsStaticObjectExpression)((_b = node.arguments) == null ? void 0 : _b[1], identifierBindings, staticFilterValueBindings);
}
function getRunJsStaticDataSourceKeyFromHeaders(headersNode, source, stringBindings, staticFilterValueBindings, identifierBindings, unresolvedAsUnknown = true, propertyUnknownAsUnknown = unresolvedAsUnknown) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(headersNode);
  if (!unwrapped) {
    return void 0;
  }
  const headers = (0, import_static_values.resolveRunJsStaticObjectExpression)(headersNode, identifierBindings, staticFilterValueBindings);
  if (!headers) {
    return unresolvedAsUnknown ? "" : void 0;
  }
  const dataSourceProperty = (0, import_static_values.getRunJsObjectPropertyLookup)(
    headers,
    ["x-data-source"],
    identifierBindings,
    staticFilterValueBindings
  );
  if (dataSourceProperty.status === "unknown") {
    return propertyUnknownAsUnknown ? "" : void 0;
  }
  if (dataSourceProperty.status === "missing") {
    return void 0;
  }
  if (dataSourceProperty.status !== "found") {
    return void 0;
  }
  return (0, import_static_values.resolveRunJsStaticString)(dataSourceProperty.property.value, source, stringBindings, identifierBindings) ?? "";
}
function getRunJsStaticRequestDataSourceKeyFromAst(node, source, stringBindings, staticFilterValueBindings, identifierBindings) {
  const configArg = getRunJsStaticRequestConfigObjectFromAst(node, identifierBindings, staticFilterValueBindings);
  const headersProperty = (0, import_static_values.getRunJsObjectProperty)(configArg, ["headers"], identifierBindings, staticFilterValueBindings);
  return getRunJsStaticDataSourceKeyFromHeaders(
    headersProperty == null ? void 0 : headersProperty.value,
    source,
    stringBindings,
    staticFilterValueBindings,
    identifierBindings
  );
}
function getRunJsApiResourceCallDataSourceKey(args, source, stringBindings, staticFilterValueBindings, identifierBindings) {
  const actionName = (0, import_static_values.resolveRunJsStaticString)(args == null ? void 0 : args[1], source, stringBindings, identifierBindings);
  if (actionName) {
    return getRunJsStaticDataSourceKeyFromActionParams(
      args == null ? void 0 : args[2],
      source,
      stringBindings,
      staticFilterValueBindings,
      identifierBindings
    );
  }
  const thirdArgDataSourceKey = getRunJsStaticDataSourceKeyFromHeaders(
    args == null ? void 0 : args[2],
    source,
    stringBindings,
    staticFilterValueBindings,
    identifierBindings
  );
  if (typeof thirdArgDataSourceKey === "string") {
    return thirdArgDataSourceKey;
  }
  return getRunJsStaticDataSourceKeyFromHeaders(
    args == null ? void 0 : args[1],
    source,
    stringBindings,
    staticFilterValueBindings,
    identifierBindings,
    false
  );
}
function getRunJsStaticDataSourceKeyFromActionParams(paramsNode, source, stringBindings, staticFilterValueBindings, identifierBindings) {
  const params = (0, import_static_values.resolveRunJsStaticObjectExpression)(paramsNode, identifierBindings, staticFilterValueBindings);
  if (params) {
    const dataSourceProperty = getRunJsActionParamsDataSourcePropertyLookup(
      params,
      identifierBindings,
      staticFilterValueBindings
    );
    if (dataSourceProperty.status === "unknown") {
      return "";
    }
    if (dataSourceProperty.status === "found") {
      return (0, import_static_values.resolveRunJsStaticString)(dataSourceProperty.property.value, source, stringBindings, identifierBindings) ?? "";
    }
    return void 0;
  }
  return hasRunJsInactiveStaticDataSourceParamBinding(paramsNode, staticFilterValueBindings, identifierBindings) ? "" : void 0;
}
function getRunJsActionParamsDataSourcePropertyLookup(node, identifierBindings, staticFilterValueBindings) {
  const object = (0, import_bindings.unwrapAstChainExpression)(node);
  if ((object == null ? void 0 : object.type) !== "ObjectExpression") {
    return { status: "missing" };
  }
  let hasUnknownLaterOverride = false;
  const properties = object.properties || [];
  for (let index = properties.length - 1; index >= 0; index -= 1) {
    const property = properties[index];
    if (!property) {
      continue;
    }
    if (property.type === "SpreadElement") {
      const spreadObject = (0, import_static_values.resolveRunJsStaticObjectExpression)(
        property.argument,
        identifierBindings,
        staticFilterValueBindings
      );
      if (spreadObject) {
        const spreadLookup = getRunJsActionParamsDataSourcePropertyLookup(
          spreadObject,
          identifierBindings,
          staticFilterValueBindings
        );
        if (spreadLookup.status === "found") {
          return hasUnknownLaterOverride ? { status: "unknown" } : spreadLookup;
        }
        if (spreadLookup.status === "unknown") {
          hasUnknownLaterOverride = true;
        }
        continue;
      }
      hasUnknownLaterOverride = true;
      continue;
    }
    if (property.type !== "Property") {
      hasUnknownLaterOverride = true;
      continue;
    }
    const propertyName = (0, import_surface.normalizeText)((0, import_static_values.getAstStaticPropertyName)(property));
    if (!propertyName) {
      hasUnknownLaterOverride = true;
      continue;
    }
    if (propertyName.toLowerCase() === "x-data-source") {
      return hasUnknownLaterOverride ? { status: "unknown" } : { status: "found", property };
    }
  }
  return { status: "missing" };
}
function hasRunJsInactiveStaticDataSourceParamBinding(node, staticFilterValueBindings, identifierBindings) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if ((unwrapped == null ? void 0 : unwrapped.type) !== "Identifier") {
    return false;
  }
  const index = typeof unwrapped.start === "number" ? unwrapped.start : 0;
  return staticFilterValueBindings.some(
    (binding) => binding.name === unwrapped.name && index >= binding.end && !(0, import_static_values.hasAstShadowBinding)(binding.name, index, binding, identifierBindings) && getRunJsActionParamsDataSourcePropertyLookup(binding.valueNode, identifierBindings, staticFilterValueBindings).status !== "missing"
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getRunJsActionParamsDataSourcePropertyLookup,
  getRunJsApiResourceCallDataSourceKey,
  getRunJsStaticDataSourceKeyFromActionParams,
  getRunJsStaticDataSourceKeyFromHeaders,
  getRunJsStaticRequestConfigObjectFromAst,
  getRunJsStaticRequestDataSourceKeyFromAst,
  hasRunJsInactiveStaticDataSourceParamBinding
});
