/**
 * 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 react_values_exports = {};
__export(react_values_exports, {
  getAstDefaultMemberAliasBase: () => getAstDefaultMemberAliasBase,
  getDirectReactDefaultCapabilityFromAstAliasName: () => getDirectReactDefaultCapabilityFromAstAliasName,
  getDirectReactNamespaceCapabilityFromAst: () => getDirectReactNamespaceCapabilityFromAst,
  getDirectReactNamespaceCapabilityFromAstAliasName: () => getDirectReactNamespaceCapabilityFromAstAliasName,
  getReactDefaultCapabilityFromAstPatternFallbackSources: () => getReactDefaultCapabilityFromAstPatternFallbackSources,
  getReactDefaultNamespaceCapabilityFromAst: () => getReactDefaultNamespaceCapabilityFromAst,
  getReactDefaultNamespaceCapabilityFromAstPatternSource: () => getReactDefaultNamespaceCapabilityFromAstPatternSource,
  getReactNamespaceCapabilityFromAst: () => getReactNamespaceCapabilityFromAst,
  getReactNamespaceCapabilityFromAstPatternFallbackSources: () => getReactNamespaceCapabilityFromAstPatternFallbackSources,
  getReactNamespaceCapabilityFromAstPatternSource: () => getReactNamespaceCapabilityFromAstPatternSource,
  isAstPatternDefaultValueSuppressedByReactNamespace: () => isAstPatternDefaultValueSuppressedByReactNamespace
});
module.exports = __toCommonJS(react_values_exports);
var import_bindings = require("./bindings");
var import_static_values = require("./static-values");
function getReactNamespaceCapabilityFromAstPatternSource(sourceTarget, identifierBindings, aliases, ctxRootAliases) {
  if (sourceTarget == null ? void 0 : sourceTarget.ambiguous) {
    return "";
  }
  if (sourceTarget == null ? void 0 : sourceTarget.node) {
    const capability = getReactNamespaceCapabilityFromAst(
      sourceTarget.node,
      identifierBindings,
      aliases,
      ctxRootAliases
    );
    if (capability) {
      return capability;
    }
  }
  const fallbackCapability = getReactNamespaceCapabilityFromAstPatternFallbackSources(
    sourceTarget,
    identifierBindings,
    aliases,
    ctxRootAliases
  );
  if (fallbackCapability) {
    return fallbackCapability;
  }
  if (!(sourceTarget == null ? void 0 : sourceTarget.aliasName)) {
    return "";
  }
  const directCapability = getDirectReactNamespaceCapabilityFromAstAliasName(sourceTarget, identifierBindings);
  if (directCapability) {
    return directCapability;
  }
  const alias = (0, import_static_values.resolveAstNamedAliasBinding)(
    sourceTarget.aliasName,
    sourceTarget.index || 0,
    sourceTarget.rootName,
    aliases,
    identifierBindings
  );
  return (alias == null ? void 0 : alias.capability) || "";
}
function getReactDefaultNamespaceCapabilityFromAstPatternSource(sourceTarget, identifierBindings, aliases, ctxRootAliases = [], defaultAliases = []) {
  if (sourceTarget == null ? void 0 : sourceTarget.node) {
    const capability = getReactDefaultNamespaceCapabilityFromAst(
      sourceTarget.node,
      identifierBindings,
      aliases,
      ctxRootAliases,
      defaultAliases
    );
    if (capability) {
      return capability;
    }
  }
  const fallbackCapability = getReactDefaultCapabilityFromAstPatternFallbackSources(
    sourceTarget,
    identifierBindings,
    aliases,
    ctxRootAliases,
    defaultAliases
  );
  if (fallbackCapability) {
    return fallbackCapability;
  }
  if (!(sourceTarget == null ? void 0 : sourceTarget.aliasName)) {
    return "";
  }
  const directCapability = getDirectReactDefaultCapabilityFromAstAliasName(sourceTarget, identifierBindings);
  if (directCapability) {
    return directCapability;
  }
  const exactAlias = (0, import_static_values.resolveAstNamedAliasBinding)(
    sourceTarget.aliasName,
    sourceTarget.index || 0,
    sourceTarget.rootName,
    defaultAliases,
    identifierBindings
  );
  if (exactAlias) {
    return exactAlias.capability;
  }
  const dynamicAlias = (0, import_static_values.resolveAstDynamicAliasBinding)(
    sourceTarget.aliasName,
    sourceTarget.index || 0,
    sourceTarget.rootName,
    defaultAliases,
    identifierBindings
  );
  const exactNamespaceAlias = (0, import_static_values.resolveAstNamedAliasBinding)(
    sourceTarget.aliasName,
    sourceTarget.index || 0,
    sourceTarget.rootName,
    aliases,
    identifierBindings
  );
  if (dynamicAlias && (!exactNamespaceAlias || (0, import_static_values.compareAstAliasPrecedence)(dynamicAlias, exactNamespaceAlias) >= 0)) {
    return dynamicAlias.capability;
  }
  const exactNamespaceCapability = getReactNamespaceCapabilityFromAstPatternSource(
    sourceTarget,
    identifierBindings,
    aliases,
    ctxRootAliases
  );
  if (exactNamespaceCapability) {
    return "";
  }
  if (dynamicAlias) {
    return dynamicAlias.capability;
  }
  const namespaceAliasName = getAstDefaultMemberAliasBase(sourceTarget.aliasName);
  if (!namespaceAliasName) {
    return "";
  }
  const directNamespaceCapability = getDirectReactNamespaceCapabilityFromAstAliasName(
    {
      ...sourceTarget,
      aliasName: namespaceAliasName
    },
    identifierBindings
  );
  if (directNamespaceCapability) {
    return `${directNamespaceCapability}.default`;
  }
  const namespaceAlias = (0, import_static_values.resolveAstNamedAliasBinding)(
    namespaceAliasName,
    sourceTarget.index || 0,
    sourceTarget.rootName,
    aliases,
    identifierBindings
  );
  return namespaceAlias ? `${namespaceAlias.capability}.default` : "";
}
function getReactNamespaceCapabilityFromAstPatternFallbackSources(sourceTarget, identifierBindings, aliases, ctxRootAliases) {
  for (const fallbackTarget of (sourceTarget == null ? void 0 : sourceTarget.fallbackSourceTargets) || []) {
    const capability = getReactNamespaceCapabilityFromAstPatternSource(
      fallbackTarget,
      identifierBindings,
      aliases,
      ctxRootAliases
    );
    if (capability) {
      return capability;
    }
  }
  return "";
}
function getReactDefaultCapabilityFromAstPatternFallbackSources(sourceTarget, identifierBindings, aliases, ctxRootAliases, defaultAliases) {
  for (const fallbackTarget of (sourceTarget == null ? void 0 : sourceTarget.fallbackSourceTargets) || []) {
    const capability = getReactDefaultNamespaceCapabilityFromAstPatternSource(
      fallbackTarget,
      identifierBindings,
      aliases,
      ctxRootAliases,
      defaultAliases
    );
    if (capability) {
      return capability;
    }
    const namespaceCapability = getReactNamespaceCapabilityFromAstPatternSource(
      fallbackTarget,
      identifierBindings,
      aliases,
      ctxRootAliases
    );
    if (namespaceCapability) {
      return "";
    }
  }
  return "";
}
function getAstDefaultMemberAliasBase(aliasName) {
  const suffix = ".default";
  return aliasName.endsWith(suffix) ? aliasName.slice(0, -suffix.length) : "";
}
function getDirectReactNamespaceCapabilityFromAstAliasName(sourceTarget, identifierBindings) {
  const aliasName = String((sourceTarget == null ? void 0 : sourceTarget.aliasName) || "");
  const index = (sourceTarget == null ? void 0 : sourceTarget.index) || 0;
  if (aliasName === "React" && !(0, import_static_values.hasAstActiveBinding)("React", index, identifierBindings)) {
    return "React";
  }
  if (aliasName === "ctx.React" && sourceTarget.rootName === "ctx" && !(0, import_static_values.hasAstActiveBinding)("ctx", index, identifierBindings)) {
    return "ctx.React";
  }
  if (aliasName === "ctx.libs.React" && sourceTarget.rootName === "ctx" && !(0, import_static_values.hasAstActiveBinding)("ctx", index, identifierBindings)) {
    return "ctx.libs.React";
  }
  return "";
}
function getDirectReactDefaultCapabilityFromAstAliasName(sourceTarget, identifierBindings) {
  const namespaceAliasName = getAstDefaultMemberAliasBase(String((sourceTarget == null ? void 0 : sourceTarget.aliasName) || ""));
  if (!namespaceAliasName) {
    return "";
  }
  const namespaceCapability = getDirectReactNamespaceCapabilityFromAstAliasName(
    {
      ...sourceTarget,
      aliasName: namespaceAliasName
    },
    identifierBindings
  );
  return namespaceCapability ? `${namespaceCapability}.default` : "";
}
function isAstPatternDefaultValueSuppressedByReactNamespace(sourceTarget, identifierBindings, namespaceAliases, ctxRootAliases) {
  if (!sourceTarget) {
    return false;
  }
  return Boolean(
    getReactNamespaceCapabilityFromAstPatternSource(sourceTarget, identifierBindings, namespaceAliases, ctxRootAliases)
  );
}
function getReactNamespaceCapabilityFromAst(node, identifierBindings, aliases, ctxRootAliases = []) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return "";
  }
  if (unwrapped.type === "ConditionalExpression") {
    return getReactNamespaceCapabilityFromAst(unwrapped.consequent, identifierBindings, aliases, ctxRootAliases) || getReactNamespaceCapabilityFromAst(unwrapped.alternate, identifierBindings, aliases, ctxRootAliases);
  }
  if (unwrapped.type === "LogicalExpression") {
    return getReactNamespaceCapabilityFromAst(unwrapped.left, identifierBindings, aliases, ctxRootAliases) || getReactNamespaceCapabilityFromAst(unwrapped.right, identifierBindings, aliases, ctxRootAliases);
  }
  if (unwrapped.type === "SequenceExpression") {
    const expressions = unwrapped.expressions || [];
    return getReactNamespaceCapabilityFromAst(
      expressions[expressions.length - 1],
      identifierBindings,
      aliases,
      ctxRootAliases
    );
  }
  if (unwrapped.type === "AssignmentExpression" && (0, import_static_values.isAstCtxApiAliasAssignmentOperator)(unwrapped.operator)) {
    return getReactNamespaceCapabilityFromAst(unwrapped.right, identifierBindings, aliases, ctxRootAliases);
  }
  if (unwrapped.type === "Identifier") {
    const index = typeof unwrapped.start === "number" ? unwrapped.start : 0;
    const alias = (0, import_static_values.resolveAstAliasBinding)(unwrapped.name, index, aliases, identifierBindings);
    if (alias) {
      return alias.capability;
    }
  }
  if (unwrapped.type === "MemberExpression") {
    const alias = (0, import_static_values.resolveAstMemberAliasBinding)(unwrapped, aliases, identifierBindings);
    if (alias) {
      return alias.capability;
    }
  }
  return getDirectReactNamespaceCapabilityFromAst(unwrapped, identifierBindings, ctxRootAliases);
}
function getReactDefaultNamespaceCapabilityFromAst(node, identifierBindings, aliases, ctxRootAliases = [], defaultAliases = []) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return "";
  }
  if (unwrapped.type === "ConditionalExpression") {
    return getReactDefaultNamespaceCapabilityFromAst(
      unwrapped.consequent,
      identifierBindings,
      aliases,
      ctxRootAliases,
      defaultAliases
    ) || getReactDefaultNamespaceCapabilityFromAst(
      unwrapped.alternate,
      identifierBindings,
      aliases,
      ctxRootAliases,
      defaultAliases
    );
  }
  if (unwrapped.type === "LogicalExpression") {
    return getReactDefaultNamespaceCapabilityFromAst(
      unwrapped.left,
      identifierBindings,
      aliases,
      ctxRootAliases,
      defaultAliases
    ) || getReactDefaultNamespaceCapabilityFromAst(
      unwrapped.right,
      identifierBindings,
      aliases,
      ctxRootAliases,
      defaultAliases
    );
  }
  if (unwrapped.type === "SequenceExpression") {
    const expressions = unwrapped.expressions || [];
    return getReactDefaultNamespaceCapabilityFromAst(
      expressions[expressions.length - 1],
      identifierBindings,
      aliases,
      ctxRootAliases,
      defaultAliases
    );
  }
  if (unwrapped.type === "AssignmentExpression" && (0, import_static_values.isAstCtxApiAliasAssignmentOperator)(unwrapped.operator)) {
    return getReactDefaultNamespaceCapabilityFromAst(
      unwrapped.right,
      identifierBindings,
      aliases,
      ctxRootAliases,
      defaultAliases
    );
  }
  if (unwrapped.type === "Identifier") {
    const index = typeof unwrapped.start === "number" ? unwrapped.start : 0;
    const alias = (0, import_static_values.resolveAstAliasBinding)(unwrapped.name, index, defaultAliases, identifierBindings);
    return (alias == null ? void 0 : alias.capability) || "";
  }
  if (unwrapped.type === "MemberExpression") {
    const alias = (0, import_static_values.resolveAstMemberAliasBinding)(unwrapped, defaultAliases, identifierBindings);
    if (alias) {
      return alias.capability;
    }
  }
  if (unwrapped.type !== "MemberExpression" || (0, import_static_values.getAstStaticPropertyName)(unwrapped) !== "default") {
    return "";
  }
  const namespace = getReactNamespaceCapabilityFromAst(unwrapped.object, identifierBindings, aliases, ctxRootAliases);
  return namespace ? `${namespace}.default` : "";
}
function getDirectReactNamespaceCapabilityFromAst(node, identifierBindings, ctxRootAliases = []) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return "";
  }
  if (unwrapped.type === "Identifier" && unwrapped.name === "React") {
    const index = typeof unwrapped.start === "number" ? unwrapped.start : 0;
    return (0, import_static_values.hasAstActiveBinding)("React", index, identifierBindings) ? "" : "React";
  }
  if (unwrapped.type !== "MemberExpression") {
    return "";
  }
  const propertyName = (0, import_static_values.getAstStaticPropertyName)(unwrapped);
  if (propertyName !== "React") {
    return "";
  }
  if ((0, import_static_values.isCtxRootFromAst)(unwrapped.object, ctxRootAliases, identifierBindings)) {
    return "ctx.React";
  }
  const object = (0, import_bindings.unwrapAstChainExpression)(unwrapped.object);
  if ((object == null ? void 0 : object.type) === "MemberExpression" && (0, import_static_values.getAstStaticPropertyName)(object) === "libs" && (0, import_static_values.isCtxRootFromAst)(object.object, ctxRootAliases, identifierBindings)) {
    return "ctx.libs.React";
  }
  return "";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getAstDefaultMemberAliasBase,
  getDirectReactDefaultCapabilityFromAstAliasName,
  getDirectReactNamespaceCapabilityFromAst,
  getDirectReactNamespaceCapabilityFromAstAliasName,
  getReactDefaultCapabilityFromAstPatternFallbackSources,
  getReactDefaultNamespaceCapabilityFromAst,
  getReactDefaultNamespaceCapabilityFromAstPatternSource,
  getReactNamespaceCapabilityFromAst,
  getReactNamespaceCapabilityFromAstPatternFallbackSources,
  getReactNamespaceCapabilityFromAstPatternSource,
  isAstPatternDefaultValueSuppressedByReactNamespace
});
