/**
 * 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 static_values_exports = {};
__export(static_values_exports, {
  addAstFunctionParamBindings: () => addAstFunctionParamBindings,
  collectAstArrayRestCarrierSourceTargets: () => collectAstArrayRestCarrierSourceTargets,
  collectAstArraySpreadCarrierCopies: () => collectAstArraySpreadCarrierCopies,
  collectAstCarrierCandidateMembers: () => collectAstCarrierCandidateMembers,
  collectAstCarrierSourceTargets: () => collectAstCarrierSourceTargets,
  collectAstDynamicCarrierSourceTargets: () => collectAstDynamicCarrierSourceTargets,
  collectAstKnownCarrierMembers: () => collectAstKnownCarrierMembers,
  collectAstLiteralSpreadCarrierCopies: () => collectAstLiteralSpreadCarrierCopies,
  collectAstObjectPatternAliases: () => collectAstObjectPatternAliases,
  collectAstObjectPatternPathAliases: () => collectAstObjectPatternPathAliases,
  collectAstObjectPatternStaticMembers: () => collectAstObjectPatternStaticMembers,
  collectAstObjectRestCarrierSourceTargets: () => collectAstObjectRestCarrierSourceTargets,
  collectAstObjectSpreadCarrierCopies: () => collectAstObjectSpreadCarrierCopies,
  collectAstPatternBindingIdentifiers: () => collectAstPatternBindingIdentifiers,
  collectAstPatternCarrierSourceTargets: () => collectAstPatternCarrierSourceTargets,
  collectAstPatternCarrierSourceTargetsFromSourceTarget: () => collectAstPatternCarrierSourceTargetsFromSourceTarget,
  collectAstPatternDefaultValueAliases: () => collectAstPatternDefaultValueAliases,
  collectAstPatternDefaultValueAliasesWithSource: () => collectAstPatternDefaultValueAliasesWithSource,
  collectAstPatternMemberExpressions: () => collectAstPatternMemberExpressions,
  collectAstPatternRestCarrierSourceTargets: () => collectAstPatternRestCarrierSourceTargets,
  collectAstPatternRestCarrierSourceTargetsRec: () => collectAstPatternRestCarrierSourceTargetsRec,
  collectAstPatternSourceTargets: () => collectAstPatternSourceTargets,
  collectAstPatternSourceTargetsRec: () => collectAstPatternSourceTargetsRec,
  collectAstRestCarrierAliasCopies: () => collectAstRestCarrierAliasCopies,
  collectAstStaticCarrierMemberSourceTargets: () => collectAstStaticCarrierMemberSourceTargets,
  collectAstStaticCarrierMembers: () => collectAstStaticCarrierMembers,
  compareAstAliasPrecedence: () => compareAstAliasPrecedence,
  createAstAmbiguousSourceTarget: () => createAstAmbiguousSourceTarget,
  createAstFallbackSourceTarget: () => createAstFallbackSourceTarget,
  createAstPatternSourceTarget: () => createAstPatternSourceTarget,
  createMaybeAstPatternSourceTarget: () => createMaybeAstPatternSourceTarget,
  dedupeAstResourceEntries: () => dedupeAstResourceEntries,
  findUnboundCtxMatches: () => findUnboundCtxMatches,
  getAstAliasPrecedenceStart: () => getAstAliasPrecedenceStart,
  getAstAliasRootName: () => getAstAliasRootName,
  getAstArrayCarrierMemberSourceTarget: () => getAstArrayCarrierMemberSourceTarget,
  getAstAssignmentTargetScope: () => getAstAssignmentTargetScope,
  getAstBindingIdentifierName: () => getAstBindingIdentifierName,
  getAstBindingIdentifierNode: () => getAstBindingIdentifierNode,
  getAstMemberAliasLookup: () => getAstMemberAliasLookup,
  getAstMemberAssignmentTargetScope: () => getAstMemberAssignmentTargetScope,
  getAstMemberRootIdentifier: () => getAstMemberRootIdentifier,
  getAstMemberWriteLookup: () => getAstMemberWriteLookup,
  getAstObjectCarrierMemberSourceTarget: () => getAstObjectCarrierMemberSourceTarget,
  getAstObjectPatternFromValue: () => getAstObjectPatternFromValue,
  getAstPatternMemberSourceTarget: () => getAstPatternMemberSourceTarget,
  getAstPatternSourceRoot: () => getAstPatternSourceRoot,
  getAstPatternTargetAliasLookup: () => getAstPatternTargetAliasLookup,
  getAstSpreadMemberSourceTarget: () => getAstSpreadMemberSourceTarget,
  getAstStaticArrayElementCount: () => getAstStaticArrayElementCount,
  getAstStaticCarrierMemberSourceTarget: () => getAstStaticCarrierMemberSourceTarget,
  getAstStaticCarrierMemberValue: () => getAstStaticCarrierMemberValue,
  getAstStaticMemberKey: () => getAstStaticMemberKey,
  getAstStaticPropertyName: () => getAstStaticPropertyName,
  getCtxMethodName: () => getCtxMethodName,
  getRunJsObjectProperty: () => getRunJsObjectProperty,
  getRunJsObjectPropertyByPath: () => getRunJsObjectPropertyByPath,
  getRunJsObjectPropertyLookup: () => getRunJsObjectPropertyLookup,
  hasAstActiveBinding: () => hasAstActiveBinding,
  hasAstShadowBinding: () => hasAstShadowBinding,
  isAstCtxApiAliasAssignmentOperator: () => isAstCtxApiAliasAssignmentOperator,
  isAstDefiniteAssignmentOperator: () => isAstDefiniteAssignmentOperator,
  isAstDynamicMemberAliasMatch: () => isAstDynamicMemberAliasMatch,
  isCtxIdentifier: () => isCtxIdentifier,
  isCtxRootFromAst: () => isCtxRootFromAst,
  isUnshadowedCtxIdentifier: () => isUnshadowedCtxIdentifier,
  resolveAstActiveIdentifierBinding: () => resolveAstActiveIdentifierBinding,
  resolveAstAliasBinding: () => resolveAstAliasBinding,
  resolveAstDynamicAliasBinding: () => resolveAstDynamicAliasBinding,
  resolveAstMemberAliasBinding: () => resolveAstMemberAliasBinding,
  resolveAstNamedAliasBinding: () => resolveAstNamedAliasBinding,
  resolveAstResourceTypeExpression: () => resolveAstResourceTypeExpression,
  resolveAstStaticStringValue: () => resolveAstStaticStringValue,
  resolveAstStaticTemplateLiteralValue: () => resolveAstStaticTemplateLiteralValue,
  resolveCtxMethodCall: () => resolveCtxMethodCall,
  resolveRunJsStaticObjectExpression: () => resolveRunJsStaticObjectExpression,
  resolveRunJsStaticObjectExpressionRec: () => resolveRunJsStaticObjectExpressionRec,
  resolveRunJsStaticString: () => resolveRunJsStaticString,
  trimAstAliasesAfterWrites: () => trimAstAliasesAfterWrites
});
module.exports = __toCommonJS(static_values_exports);
var import_internal_types = require("../internal-types");
var import_constants = require("../runtime/constants");
var import_surface = require("../runtime/surface");
var import_bindings = require("./bindings");
var import_source = require("./source");
function resolveCtxMethodCall(node, aliases, identifierBindings) {
  const callee = (0, import_bindings.unwrapAstChainExpression)(node.callee);
  const directMethod = getCtxMethodName(callee, identifierBindings);
  if (directMethod) {
    return {
      capability: `ctx.${directMethod}`,
      method: directMethod
    };
  }
  if ((callee == null ? void 0 : callee.type) === "Identifier") {
    const alias = resolveAstAliasBinding(callee.name, node.start || 0, aliases, identifierBindings);
    if (alias) {
      return alias;
    }
  }
  return void 0;
}
function getCtxMethodName(node, identifierBindings) {
  const member = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!member || member.type !== "MemberExpression" || !isUnshadowedCtxIdentifier(member.object, identifierBindings)) {
    return "";
  }
  const propertyName = getAstStaticPropertyName(member);
  return import_constants.AST_CTX_METHOD_NAMES.has(propertyName) ? propertyName : "";
}
function getAstStaticPropertyName(node) {
  const property = (node == null ? void 0 : node.key) || (node == null ? void 0 : node.property);
  if (!property) {
    return "";
  }
  if (!node.computed && property.type === "Identifier") {
    return property.name;
  }
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(property);
  if ((unwrapped == null ? void 0 : unwrapped.type) === "Literal" && (typeof unwrapped.value === "string" || typeof unwrapped.value === "number")) {
    return String(unwrapped.value);
  }
  const value = resolveAstStaticStringValue(property, "");
  return typeof value === "string" ? value : "";
}
function getAstStaticMemberKey(node) {
  const property = (node == null ? void 0 : node.key) || (node == null ? void 0 : node.property);
  if (!property) {
    return "";
  }
  if (!node.computed && property.type === "Identifier") {
    return property.name;
  }
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(property);
  if ((unwrapped == null ? void 0 : unwrapped.type) === "Literal") {
    if (typeof unwrapped.value === "string" || typeof unwrapped.value === "number") {
      return String(unwrapped.value);
    }
    return "";
  }
  const value = resolveAstStaticStringValue(property, "");
  return typeof value === "string" ? value : "";
}
function isCtxIdentifier(node) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  return (unwrapped == null ? void 0 : unwrapped.type) === "Identifier" && unwrapped.name === "ctx";
}
function isUnshadowedCtxIdentifier(node, identifierBindings) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!isCtxIdentifier(unwrapped)) {
    return false;
  }
  const index = typeof unwrapped.start === "number" ? unwrapped.start : 0;
  return !hasAstActiveBinding("ctx", index, identifierBindings);
}
function isCtxRootFromAst(node, aliases, identifierBindings) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return false;
  }
  if (unwrapped.type === "ConditionalExpression") {
    return isCtxRootFromAst(unwrapped.consequent, aliases, identifierBindings) || isCtxRootFromAst(unwrapped.alternate, aliases, identifierBindings);
  }
  if (unwrapped.type === "LogicalExpression") {
    return isCtxRootFromAst(unwrapped.left, aliases, identifierBindings) || isCtxRootFromAst(unwrapped.right, aliases, identifierBindings);
  }
  if (unwrapped.type === "SequenceExpression") {
    const expressions = unwrapped.expressions || [];
    return isCtxRootFromAst(expressions[expressions.length - 1], aliases, identifierBindings);
  }
  if (unwrapped.type === "AssignmentExpression" && isAstCtxApiAliasAssignmentOperator(unwrapped.operator)) {
    return isCtxRootFromAst(unwrapped.right, aliases, identifierBindings);
  }
  if (isUnshadowedCtxIdentifier(unwrapped, identifierBindings)) {
    return true;
  }
  if (unwrapped.type !== "Identifier") {
    return false;
  }
  const index = typeof unwrapped.start === "number" ? unwrapped.start : 0;
  return !!resolveAstAliasBinding(unwrapped.name, index, aliases, identifierBindings);
}
function hasAstActiveBinding(name, index, identifierBindings) {
  return identifierBindings.some((binding) => binding.name === name && index >= binding.start && index < binding.end);
}
function isAstCtxApiAliasAssignmentOperator(operator) {
  return operator === "=" || operator === "||=" || operator === "&&=" || operator === "??=";
}
function isAstDefiniteAssignmentOperator(operator) {
  return operator === "=" || operator === "&&=";
}
function getAstAssignmentTargetScope(target, ancestors, sourceLength, identifierBindings) {
  const fallbackScope = (0, import_bindings.getAstBindingScopeRange)(ancestors, sourceLength);
  if ((target == null ? void 0 : target.type) === "Identifier") {
    const index = typeof target.start === "number" ? target.start : 0;
    const binding = resolveAstActiveIdentifierBinding(target.name, index, identifierBindings);
    if (binding) {
      const executionScope = (0, import_bindings.getAstExecutionScopeRange)(ancestors, sourceLength);
      if (binding.start < executionScope.start && binding.end >= executionScope.end) {
        return fallbackScope;
      }
      return {
        start: binding.start,
        end: binding.end
      };
    }
  }
  return fallbackScope;
}
function getAstMemberAssignmentTargetScope(target, ancestors, sourceLength, identifierBindings) {
  const fallbackScope = (0, import_bindings.getAstBindingScopeRange)(ancestors, sourceLength);
  const lookup = getAstMemberAliasLookup(target);
  if (!lookup) {
    return fallbackScope;
  }
  const binding = resolveAstActiveIdentifierBinding(lookup.rootName, lookup.index, identifierBindings);
  if (!binding) {
    return fallbackScope;
  }
  const executionScope = (0, import_bindings.getAstExecutionScopeRange)(ancestors, sourceLength);
  if (binding.start < executionScope.start && binding.end >= executionScope.end) {
    return fallbackScope;
  }
  return {
    start: binding.start,
    end: binding.end
  };
}
function findUnboundCtxMatches(masked, pattern, bindings) {
  return (0, import_source.findMatches)(masked, pattern).filter((entry) => !(0, import_bindings.isNameBoundAtIndex)(bindings, "ctx", entry.index));
}
function dedupeAstResourceEntries(entries) {
  const seen = /* @__PURE__ */ new Set();
  return entries.filter((entry) => {
    const key = `${entry.ruleId}:${entry.capability}:${entry.index}`;
    if (seen.has(key)) {
      return false;
    }
    seen.add(key);
    return true;
  }).sort((left, right) => left.index - right.index);
}
function resolveAstResourceTypeExpression(node, source, stringBindings, identifierBindings) {
  const resolved = resolveAstStaticStringValue(node, source);
  if (typeof resolved === "string") {
    return { status: "resolved", value: resolved };
  }
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if ((unwrapped == null ? void 0 : unwrapped.type) === "Identifier") {
    const binding = resolveAstAliasBinding(unwrapped.name, unwrapped.start || 0, stringBindings, identifierBindings);
    if (binding) {
      return { status: "resolved", value: binding.value };
    }
  }
  if ((unwrapped == null ? void 0 : unwrapped.type) === "MemberExpression") {
    const binding = resolveAstMemberAliasBinding(unwrapped, stringBindings, identifierBindings);
    if (binding) {
      return { status: "resolved", value: binding.value };
    }
  }
  const templateValue = resolveAstStaticTemplateLiteralValue(unwrapped, source, stringBindings, identifierBindings);
  if (typeof templateValue === "string") {
    return { status: "resolved", value: templateValue };
  }
  return {
    status: "unresolved",
    expression: source.slice((node == null ? void 0 : node.start) || 0, (node == null ? void 0 : node.end) || (node == null ? void 0 : node.start) || 0).trim()
  };
}
function resolveAstStaticStringValue(node, source) {
  var _a, _b, _c, _d;
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return void 0;
  }
  if (unwrapped.type === "Literal" && typeof unwrapped.value === "string") {
    return unwrapped.value;
  }
  if (unwrapped.type === "TemplateLiteral" && !((_a = unwrapped.expressions) == null ? void 0 : _a.length)) {
    return ((_d = (_c = (_b = unwrapped.quasis) == null ? void 0 : _b[0]) == null ? void 0 : _c.value) == null ? void 0 : _d.cooked) ?? source.slice(unwrapped.start + 1, unwrapped.end - 1);
  }
  return void 0;
}
function resolveAstStaticTemplateLiteralValue(node, source, stringBindings, identifierBindings) {
  var _a, _b;
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if ((unwrapped == null ? void 0 : unwrapped.type) !== "TemplateLiteral" || !(unwrapped.expressions || []).length) {
    return void 0;
  }
  const quasis = unwrapped.quasis || [];
  const expressions = unwrapped.expressions || [];
  let value = "";
  for (let index = 0; index < quasis.length; index += 1) {
    const quasi = quasis[index];
    value += ((_a = quasi == null ? void 0 : quasi.value) == null ? void 0 : _a.cooked) ?? ((_b = quasi == null ? void 0 : quasi.value) == null ? void 0 : _b.raw) ?? "";
    if (index >= expressions.length) {
      continue;
    }
    const expression = resolveAstResourceTypeExpression(expressions[index], source, stringBindings, identifierBindings);
    if (expression.status !== "resolved") {
      return void 0;
    }
    value += expression.value;
  }
  return value;
}
function resolveAstAliasBinding(name, index, aliases, identifierBindings) {
  const candidates = aliases.filter((entry) => entry.name === name && index >= entry.start && index < entry.end).sort((left, right) => right.start - left.start);
  return candidates.find((entry) => !hasAstShadowBinding(name, index, entry, identifierBindings));
}
function resolveAstMemberAliasBinding(node, aliases, identifierBindings) {
  const lookup = getAstMemberAliasLookup(node);
  if (!lookup) {
    return void 0;
  }
  return resolveAstNamedAliasBinding(lookup.aliasName, lookup.index, lookup.rootName, aliases, identifierBindings);
}
function getAstMemberAliasLookup(node) {
  const member = (0, import_bindings.unwrapAstChainExpression)(node);
  if ((member == null ? void 0 : member.type) !== "MemberExpression") {
    return void 0;
  }
  const memberKey = getAstStaticMemberKey(member);
  if (!memberKey) {
    return void 0;
  }
  const object = (0, import_bindings.unwrapAstChainExpression)(member.object);
  if ((object == null ? void 0 : object.type) === "Identifier") {
    return {
      aliasName: `${object.name}.${memberKey}`,
      index: typeof member.start === "number" ? member.start : typeof object.start === "number" ? object.start : 0,
      rootName: object.name
    };
  }
  const objectLookup = getAstMemberAliasLookup(object);
  if (!objectLookup) {
    return void 0;
  }
  return {
    aliasName: `${objectLookup.aliasName}.${memberKey}`,
    index: typeof member.start === "number" ? member.start : objectLookup.index,
    rootName: objectLookup.rootName
  };
}
function getAstMemberWriteLookup(node) {
  const staticLookup = getAstMemberAliasLookup(node);
  if (staticLookup) {
    return staticLookup;
  }
  const root = getAstMemberRootIdentifier(node);
  if (!root) {
    return void 0;
  }
  const member = (0, import_bindings.unwrapAstChainExpression)(node);
  return {
    aliasName: root.name,
    index: typeof (member == null ? void 0 : member.start) === "number" ? member.start : root.index,
    rootName: root.name
  };
}
function getAstMemberRootIdentifier(node) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return void 0;
  }
  if (unwrapped.type === "Identifier") {
    return {
      index: typeof unwrapped.start === "number" ? unwrapped.start : 0,
      name: unwrapped.name
    };
  }
  if (unwrapped.type !== "MemberExpression") {
    return void 0;
  }
  return getAstMemberRootIdentifier(unwrapped.object);
}
function getAstAliasRootName(name) {
  return String(name || "").split(".")[0] || name;
}
function resolveAstNamedAliasBinding(name, index, rootName, aliases, identifierBindings) {
  const candidates = aliases.filter((entry) => entry.name === name && index >= entry.start && index < entry.end).sort((left, right) => right.start - left.start);
  return candidates.find(
    (entry) => !hasAstShadowBinding(rootName || getAstAliasRootName(name), index, entry, identifierBindings)
  );
}
function resolveAstDynamicAliasBinding(name, index, rootName, aliases, identifierBindings) {
  const candidates = aliases.filter(
    (entry) => entry.name !== name && index >= entry.start && index < entry.end && isAstDynamicMemberAliasMatch(entry.name, name)
  ).sort((left, right) => right.start - left.start);
  return candidates.find(
    (entry) => !hasAstShadowBinding(rootName || getAstAliasRootName(name), index, entry, identifierBindings)
  );
}
function compareAstAliasPrecedence(left, right) {
  if (left.start !== right.start) {
    return left.start - right.start;
  }
  return getAstAliasPrecedenceStart(left) - getAstAliasPrecedenceStart(right);
}
function getAstAliasPrecedenceStart(alias) {
  return typeof alias.precedenceStart === "number" ? alias.precedenceStart : alias.start;
}
function isAstDynamicMemberAliasMatch(patternName, name) {
  if (!patternName.includes(import_internal_types.AST_DYNAMIC_MEMBER_ALIAS)) {
    return false;
  }
  const patternParts = patternName.split(".");
  const nameParts = name.split(".");
  return patternParts.length === nameParts.length && patternParts.every((part, index) => part === import_internal_types.AST_DYNAMIC_MEMBER_ALIAS || part === nameParts[index]);
}
function resolveAstActiveIdentifierBinding(name, index, identifierBindings) {
  return identifierBindings.filter((binding) => binding.name === name && index >= binding.start && index < binding.end).sort(
    (left, right) => left.end - left.start - (right.end - right.start) || right.start - left.start || right.end - left.end
  )[0];
}
function hasAstShadowBinding(name, index, alias, identifierBindings) {
  return identifierBindings.some(
    (binding) => binding.name === name && binding.start > alias.start && index >= binding.start && index < binding.end
  );
}
function addAstFunctionParamBindings(bindings, node, sourceLength) {
  const scope = {
    start: typeof node.start === "number" ? node.start : 0,
    end: typeof node.end === "number" ? node.end : sourceLength
  };
  for (const param of node.params || []) {
    collectAstPatternBindingIdentifiers(param, (name, bindingNode) => {
      bindings.push({
        name,
        start: typeof (bindingNode == null ? void 0 : bindingNode.start) === "number" ? bindingNode.start : scope.start,
        end: scope.end
      });
    });
  }
}
function collectAstPatternBindingIdentifiers(node, addBinding) {
  if (!node) {
    return;
  }
  if (node.type === "Identifier") {
    addBinding(node.name, node);
    return;
  }
  if (node.type === "AssignmentPattern") {
    collectAstPatternBindingIdentifiers(node.left, addBinding);
    return;
  }
  if (node.type === "RestElement") {
    collectAstPatternBindingIdentifiers(node.argument, addBinding);
    return;
  }
  if (node.type === "ArrayPattern") {
    for (const element of node.elements || []) {
      collectAstPatternBindingIdentifiers(element, addBinding);
    }
    return;
  }
  if (node.type === "ObjectPattern") {
    for (const property of node.properties || []) {
      if (!property) {
        continue;
      }
      if (property.type === "RestElement") {
        collectAstPatternBindingIdentifiers(property.argument, addBinding);
        continue;
      }
      if (property.type === "Property") {
        collectAstPatternBindingIdentifiers(property.value, addBinding);
      }
    }
  }
}
function collectAstObjectPatternAliases(pattern, addAlias) {
  for (const property of pattern.properties || []) {
    if (!property || property.type !== "Property") {
      continue;
    }
    const member = getAstStaticPropertyName(property);
    const alias = getAstBindingIdentifierName(property.value);
    const aliasNode = getAstBindingIdentifierNode(property.value);
    if (member && alias) {
      addAlias(alias, member, aliasNode);
    }
  }
}
function collectAstObjectPatternPathAliases(pattern, addAlias, parentMembers = []) {
  for (const property of pattern.properties || []) {
    if (!property || property.type !== "Property") {
      continue;
    }
    const member = getAstStaticPropertyName(property);
    if (!member) {
      continue;
    }
    const members = [...parentMembers, member];
    const alias = getAstBindingIdentifierName(property.value);
    const aliasNode = getAstBindingIdentifierNode(property.value);
    if (alias) {
      addAlias(alias, members, aliasNode);
    }
    const nestedPattern = getAstObjectPatternFromValue(property.value);
    if (nestedPattern) {
      collectAstObjectPatternPathAliases(nestedPattern, addAlias, members);
    }
  }
}
function collectAstPatternDefaultValueAliases(pattern, addAlias) {
  if (!pattern) {
    return;
  }
  if (pattern.type === "AssignmentPattern") {
    collectAstPatternBindingIdentifiers(pattern.left, (alias, aliasNode) => {
      addAlias(alias, pattern.right, aliasNode, pattern.left);
    });
    collectAstPatternDefaultValueAliases(pattern.left, addAlias);
    return;
  }
  if (pattern.type === "ObjectPattern") {
    for (const property of pattern.properties || []) {
      if (!property) {
        continue;
      }
      if (property.type === "RestElement") {
        collectAstPatternDefaultValueAliases(property.argument, addAlias);
        continue;
      }
      if (property.type === "Property") {
        collectAstPatternDefaultValueAliases(property.value, addAlias);
      }
    }
    return;
  }
  if (pattern.type === "ArrayPattern") {
    for (const element of pattern.elements || []) {
      collectAstPatternDefaultValueAliases(element, addAlias);
    }
    return;
  }
  if (pattern.type === "RestElement") {
    collectAstPatternDefaultValueAliases(pattern.argument, addAlias);
  }
}
function collectAstPatternDefaultValueAliasesWithSource(pattern, sourceTarget, addAlias) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(pattern);
  if (!unwrapped) {
    return;
  }
  if (unwrapped.type === "AssignmentPattern") {
    collectAstPatternBindingIdentifiers(unwrapped.left, (alias, aliasNode) => {
      addAlias(alias, unwrapped.right, aliasNode, unwrapped.left, sourceTarget);
    });
    collectAstPatternDefaultValueAliasesWithSource(unwrapped.left, sourceTarget, addAlias);
    return;
  }
  if (unwrapped.type === "ObjectPattern") {
    for (const property of unwrapped.properties || []) {
      if (!property) {
        continue;
      }
      if (property.type === "RestElement") {
        collectAstPatternDefaultValueAliasesWithSource(property.argument, void 0, addAlias);
        continue;
      }
      if (property.type !== "Property") {
        continue;
      }
      const member = getAstStaticPropertyName(property);
      collectAstPatternDefaultValueAliasesWithSource(
        property.value,
        member ? getAstPatternMemberSourceTarget(sourceTarget, member) : void 0,
        addAlias
      );
    }
    return;
  }
  if (unwrapped.type === "ArrayPattern") {
    for (let index = 0; index < (unwrapped.elements || []).length; index += 1) {
      collectAstPatternDefaultValueAliasesWithSource(
        unwrapped.elements[index],
        getAstPatternMemberSourceTarget(sourceTarget, String(index)),
        addAlias
      );
    }
    return;
  }
  if (unwrapped.type === "RestElement") {
    collectAstPatternDefaultValueAliasesWithSource(unwrapped.argument, void 0, addAlias);
  }
}
function collectAstPatternSourceTargets(pattern, sourceNode, visit) {
  collectAstPatternSourceTargetsRec(pattern, createAstPatternSourceTarget(sourceNode), visit);
}
function collectAstPatternCarrierSourceTargets(pattern, sourceNode, aliases, identifierBindings, visit) {
  collectAstPatternCarrierSourceTargetsFromSourceTarget(
    pattern,
    createAstPatternSourceTarget(sourceNode),
    aliases,
    identifierBindings,
    visit
  );
}
function collectAstPatternCarrierSourceTargetsFromSourceTarget(pattern, sourceTarget, aliases, identifierBindings, visit) {
  collectAstPatternSourceTargetsRec(pattern, sourceTarget, (target, patternSourceTarget) => {
    const targetLookup = getAstPatternTargetAliasLookup(target);
    if (!targetLookup) {
      return;
    }
    collectAstCarrierSourceTargets(
      target,
      targetLookup.aliasName,
      patternSourceTarget,
      aliases,
      identifierBindings,
      visit
    );
  });
}
function collectAstPatternSourceTargetsRec(pattern, sourceTarget, visit) {
  var _a;
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(pattern);
  if (!unwrapped) {
    return;
  }
  if (unwrapped.type === "Identifier" || unwrapped.type === "MemberExpression") {
    visit(unwrapped, sourceTarget);
    return;
  }
  if (unwrapped.type === "AssignmentPattern") {
    collectAstPatternSourceTargetsRec(unwrapped.left, sourceTarget, visit);
    return;
  }
  if (unwrapped.type === "RestElement") {
    collectAstPatternSourceTargetsRec(unwrapped.argument, sourceTarget, visit);
    return;
  }
  if (unwrapped.type === "ObjectPattern") {
    for (const property of unwrapped.properties || []) {
      if (!property || property.type !== "Property") {
        continue;
      }
      const member = getAstStaticPropertyName(property);
      if (!member) {
        continue;
      }
      collectAstPatternSourceTargetsRec(property.value, getAstPatternMemberSourceTarget(sourceTarget, member), visit);
    }
    return;
  }
  if (unwrapped.type === "ArrayPattern") {
    for (let index = 0; index < (unwrapped.elements || []).length; index += 1) {
      if (((_a = unwrapped.elements[index]) == null ? void 0 : _a.type) === "RestElement") {
        continue;
      }
      collectAstPatternSourceTargetsRec(
        unwrapped.elements[index],
        getAstPatternMemberSourceTarget(sourceTarget, String(index)),
        visit
      );
    }
  }
}
function collectAstPatternRestCarrierSourceTargets(pattern, sourceNode, aliases, identifierBindings, visit) {
  collectAstPatternRestCarrierSourceTargetsRec(
    pattern,
    createAstPatternSourceTarget(sourceNode),
    aliases,
    identifierBindings,
    visit
  );
}
function collectAstPatternRestCarrierSourceTargetsRec(pattern, sourceTarget, aliases, identifierBindings, visit) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(pattern);
  if (!unwrapped) {
    return;
  }
  if (unwrapped.type === "AssignmentPattern") {
    collectAstPatternRestCarrierSourceTargetsRec(unwrapped.left, sourceTarget, aliases, identifierBindings, visit);
    return;
  }
  if (unwrapped.type === "ObjectPattern") {
    const excludedMembers = collectAstObjectPatternStaticMembers(unwrapped);
    for (const property of unwrapped.properties || []) {
      if (!property) {
        continue;
      }
      if (property.type === "RestElement") {
        collectAstObjectRestCarrierSourceTargets(
          property.argument,
          sourceTarget,
          excludedMembers,
          aliases,
          identifierBindings,
          visit
        );
        continue;
      }
      if (property.type !== "Property") {
        continue;
      }
      const member = getAstStaticPropertyName(property);
      if (!member) {
        continue;
      }
      collectAstPatternRestCarrierSourceTargetsRec(
        property.value,
        getAstPatternMemberSourceTarget(sourceTarget, member),
        aliases,
        identifierBindings,
        visit
      );
    }
    return;
  }
  if (unwrapped.type === "ArrayPattern") {
    for (let index = 0; index < (unwrapped.elements || []).length; index += 1) {
      const element = unwrapped.elements[index];
      if (!element) {
        continue;
      }
      if (element.type === "RestElement") {
        collectAstArrayRestCarrierSourceTargets(
          element.argument,
          sourceTarget,
          index,
          aliases,
          identifierBindings,
          visit
        );
        continue;
      }
      collectAstPatternRestCarrierSourceTargetsRec(
        element,
        getAstPatternMemberSourceTarget(sourceTarget, String(index)),
        aliases,
        identifierBindings,
        visit
      );
    }
  }
}
function collectAstObjectRestCarrierSourceTargets(target, sourceTarget, excludedMembers, aliases, identifierBindings, visit) {
  const targetLookup = getAstPatternTargetAliasLookup(target);
  if (!targetLookup) {
    return;
  }
  collectAstCarrierCandidateMembers(sourceTarget == null ? void 0 : sourceTarget.node, aliases, identifierBindings).forEach((member) => {
    if (excludedMembers.has(member)) {
      return;
    }
    const memberSourceTarget = getAstStaticCarrierMemberSourceTarget(sourceTarget == null ? void 0 : sourceTarget.node, member);
    if (!memberSourceTarget) {
      return;
    }
    visit({
      sourceTarget: memberSourceTarget,
      target,
      targetAliasName: `${targetLookup.aliasName}.${member}`
    });
  });
  collectAstRestCarrierAliasCopies(
    sourceTarget,
    targetLookup.aliasName,
    aliases,
    identifierBindings,
    (suffix, sourceAlias) => {
      const firstMember = suffix.split(".")[0];
      if (!firstMember || excludedMembers.has(firstMember)) {
        return;
      }
      visit({
        sourceAlias,
        target,
        targetAliasName: `${targetLookup.aliasName}.${suffix}`
      });
    }
  );
  collectAstObjectSpreadCarrierCopies(
    sourceTarget == null ? void 0 : sourceTarget.node,
    targetLookup.aliasName,
    aliases,
    identifierBindings,
    (suffix, sourceAlias, spreadSourceTarget) => {
      const firstMember = suffix.split(".")[0];
      if (!firstMember || excludedMembers.has(firstMember)) {
        return;
      }
      visit({
        sourceAlias,
        sourceTarget: spreadSourceTarget,
        target,
        targetAliasName: `${targetLookup.aliasName}.${suffix}`
      });
    },
    excludedMembers
  );
}
function collectAstCarrierSourceTargets(target, targetAliasName, sourceTarget, aliases, identifierBindings, visit) {
  collectAstKnownCarrierMembers(sourceTarget == null ? void 0 : sourceTarget.node, (member, valueNode) => {
    visit({
      sourceTarget: createAstPatternSourceTarget(valueNode),
      target,
      targetAliasName: `${targetAliasName}.${member}`
    });
  });
  collectAstRestCarrierAliasCopies(
    sourceTarget,
    targetAliasName,
    aliases,
    identifierBindings,
    (suffix, sourceAlias) => {
      visit({
        sourceAlias,
        target,
        targetAliasName: `${targetAliasName}.${suffix}`
      });
    }
  );
  collectAstLiteralSpreadCarrierCopies(
    sourceTarget == null ? void 0 : sourceTarget.node,
    targetAliasName,
    aliases,
    identifierBindings,
    (suffix, sourceAlias, spreadSourceTarget) => {
      visit({
        sourceAlias,
        sourceTarget: spreadSourceTarget,
        target,
        targetAliasName: `${targetAliasName}.${suffix}`
      });
    }
  );
  collectAstDynamicCarrierSourceTargets(sourceTarget == null ? void 0 : sourceTarget.node, (suffix, dynamicSourceTarget) => {
    visit({
      sourceTarget: dynamicSourceTarget,
      target,
      targetAliasName: `${targetAliasName}.${suffix}`
    });
  });
}
function collectAstArrayRestCarrierSourceTargets(target, sourceTarget, restStartIndex, aliases, identifierBindings, visit) {
  const targetLookup = getAstPatternTargetAliasLookup(target);
  if (!targetLookup) {
    return;
  }
  const mapRestSuffix = (suffix) => {
    const [sourceIndexText, ...restSuffixParts] = suffix.split(".");
    const restSuffix = restSuffixParts.length ? `.${restSuffixParts.join(".")}` : "";
    if (sourceIndexText === import_internal_types.AST_DYNAMIC_MEMBER_ALIAS) {
      return `${import_internal_types.AST_DYNAMIC_MEMBER_ALIAS}${restSuffix}`;
    }
    const sourceIndex = Number(sourceIndexText);
    if (!Number.isInteger(sourceIndex) || sourceIndex < restStartIndex) {
      return "";
    }
    return `${sourceIndex - restStartIndex}${restSuffix}`;
  };
  collectAstKnownCarrierMembers(sourceTarget == null ? void 0 : sourceTarget.node, (member, valueNode) => {
    const sourceIndex = Number(member);
    if (!Number.isInteger(sourceIndex) || sourceIndex < restStartIndex) {
      return;
    }
    visit({
      sourceTarget: createAstPatternSourceTarget(valueNode),
      target,
      targetAliasName: `${targetLookup.aliasName}.${sourceIndex - restStartIndex}`
    });
  });
  collectAstRestCarrierAliasCopies(
    sourceTarget,
    targetLookup.aliasName,
    aliases,
    identifierBindings,
    (suffix, sourceAlias) => {
      const mappedSuffix = mapRestSuffix(suffix);
      if (!mappedSuffix) {
        return;
      }
      visit({
        sourceAlias,
        target,
        targetAliasName: `${targetLookup.aliasName}.${mappedSuffix}`
      });
    }
  );
  collectAstArraySpreadCarrierCopies(
    sourceTarget == null ? void 0 : sourceTarget.node,
    targetLookup.aliasName,
    aliases,
    identifierBindings,
    (suffix, sourceAlias, spreadSourceTarget) => {
      const mappedSuffix = mapRestSuffix(suffix);
      if (!mappedSuffix) {
        return;
      }
      visit({
        sourceAlias,
        sourceTarget: spreadSourceTarget,
        target,
        targetAliasName: `${targetLookup.aliasName}.${mappedSuffix}`
      });
    }
  );
}
function collectAstRestCarrierAliasCopies(sourceTarget, targetAliasName, aliases, identifierBindings, visit) {
  const sourceAliasName = sourceTarget == null ? void 0 : sourceTarget.aliasName;
  if (!sourceAliasName) {
    return;
  }
  const sourcePrefix = `${sourceAliasName}.`;
  const sourceIndex = sourceTarget.index || 0;
  const rootName = sourceTarget.rootName || getAstAliasRootName(sourceAliasName);
  const copied = /* @__PURE__ */ new Set();
  const sourceAliases = aliases.filter((entry) => entry.name.startsWith(sourcePrefix) && sourceIndex >= entry.start && sourceIndex < entry.end).sort((left, right) => right.start - left.start);
  for (const sourceAlias of sourceAliases) {
    const suffix = sourceAlias.name.slice(sourcePrefix.length);
    if (!suffix || copied.has(`${targetAliasName}.${suffix}`)) {
      continue;
    }
    if (hasAstShadowBinding(rootName, sourceIndex, sourceAlias, identifierBindings)) {
      continue;
    }
    copied.add(`${targetAliasName}.${suffix}`);
    visit(suffix, sourceAlias);
  }
}
function collectAstLiteralSpreadCarrierCopies(sourceNode, targetAliasName, aliases, identifierBindings, visit) {
  collectAstObjectSpreadCarrierCopies(sourceNode, targetAliasName, aliases, identifierBindings, visit);
  collectAstArraySpreadCarrierCopies(sourceNode, targetAliasName, aliases, identifierBindings, visit);
}
function collectAstCarrierCandidateMembers(sourceNode, aliases, identifierBindings) {
  const members = /* @__PURE__ */ new Set();
  collectAstStaticCarrierMembers(sourceNode, (member) => members.add(member));
  collectAstKnownCarrierMembers(sourceNode, (member) => members.add(member));
  collectAstLiteralSpreadCarrierCopies(sourceNode, "__candidate__", aliases, identifierBindings, (suffix) => {
    const firstMember = suffix.split(".")[0];
    if (firstMember) {
      members.add(firstMember);
    }
  });
  collectAstDynamicCarrierSourceTargets(sourceNode, (suffix) => {
    const firstMember = suffix.split(".")[0];
    if (firstMember) {
      members.add(firstMember);
    }
  });
  return members;
}
function collectAstObjectSpreadCarrierCopies(sourceNode, targetAliasName, aliases, identifierBindings, visit, excludedMembers = /* @__PURE__ */ new Set()) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(sourceNode);
  if ((unwrapped == null ? void 0 : unwrapped.type) !== "ObjectExpression") {
    return;
  }
  const seenMembers = new Set(excludedMembers);
  for (let index = (unwrapped.properties || []).length - 1; index >= 0; index -= 1) {
    const property = unwrapped.properties[index];
    if (!property) {
      continue;
    }
    if (property.type === "Property") {
      const member = getAstStaticMemberKey(property);
      if (member) {
        seenMembers.add(member);
      }
      continue;
    }
    if (property.type !== "SpreadElement") {
      continue;
    }
    const sourceTarget = createAstPatternSourceTarget(property.argument);
    const visitSpreadMember = (suffix, sourceAlias, spreadSourceTarget) => {
      const firstMember = suffix.split(".")[0];
      if (!firstMember || seenMembers.has(firstMember)) {
        return;
      }
      seenMembers.add(firstMember);
      visit(suffix, sourceAlias, spreadSourceTarget);
    };
    collectAstRestCarrierAliasCopies(
      sourceTarget,
      targetAliasName,
      aliases,
      identifierBindings,
      (suffix, sourceAlias) => visitSpreadMember(suffix, sourceAlias, sourceTarget)
    );
    collectAstStaticCarrierMemberSourceTargets(
      property.argument,
      (suffix, spreadSourceTarget) => visitSpreadMember(suffix, void 0, spreadSourceTarget)
    );
    collectAstDynamicCarrierSourceTargets(property.argument, (suffix, dynamicSourceTarget) => {
      visitSpreadMember(suffix, void 0, dynamicSourceTarget);
    });
  }
}
function collectAstDynamicCarrierSourceTargets(sourceNode, visit) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(sourceNode);
  if ((unwrapped == null ? void 0 : unwrapped.type) !== "ObjectExpression") {
    return;
  }
  for (const property of unwrapped.properties || []) {
    if (!property || property.type !== "Property") {
      continue;
    }
    const member = getAstStaticMemberKey(property);
    if (member) {
      continue;
    }
    visit(
      import_internal_types.AST_DYNAMIC_MEMBER_ALIAS,
      createAstAmbiguousSourceTarget(createAstPatternSourceTarget(property.value || property))
    );
  }
}
function collectAstArraySpreadCarrierCopies(sourceNode, targetAliasName, aliases, identifierBindings, visit) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(sourceNode);
  if ((unwrapped == null ? void 0 : unwrapped.type) !== "ArrayExpression") {
    return;
  }
  let concreteIndexOffset = 0;
  let hasUnboundedSpread = false;
  for (const element of unwrapped.elements || []) {
    if (!element) {
      if (!hasUnboundedSpread) {
        concreteIndexOffset += 1;
      }
      continue;
    }
    if (element.type !== "SpreadElement") {
      if (!hasUnboundedSpread) {
        concreteIndexOffset += 1;
        continue;
      }
      visit(import_internal_types.AST_DYNAMIC_MEMBER_ALIAS, void 0, createAstPatternSourceTarget(element));
      continue;
    }
    if (hasUnboundedSpread) {
      const sourceTarget2 = createAstPatternSourceTarget(element.argument);
      const mapUnboundedSpreadSuffix = (suffix) => {
        const [, ...restSuffixParts] = suffix.split(".");
        const restSuffix = restSuffixParts.length ? `.${restSuffixParts.join(".")}` : "";
        return `${import_internal_types.AST_DYNAMIC_MEMBER_ALIAS}${restSuffix}`;
      };
      collectAstRestCarrierAliasCopies(
        sourceTarget2,
        targetAliasName,
        aliases,
        identifierBindings,
        (suffix, sourceAlias) => {
          visit(mapUnboundedSpreadSuffix(suffix), sourceAlias, sourceTarget2);
        }
      );
      collectAstStaticCarrierMemberSourceTargets(element.argument, (suffix, spreadSourceTarget) => {
        visit(mapUnboundedSpreadSuffix(suffix), void 0, spreadSourceTarget);
      });
      continue;
    }
    const sourceTarget = createAstPatternSourceTarget(element.argument);
    const mapSpreadSuffix = (suffix) => {
      const [sourceIndexText, ...restSuffixParts] = suffix.split(".");
      const sourceIndex = Number(sourceIndexText);
      if (!Number.isInteger(sourceIndex) || sourceIndex < 0) {
        return "";
      }
      const restSuffix = restSuffixParts.length ? `.${restSuffixParts.join(".")}` : "";
      const targetIndex = hasUnboundedSpread ? sourceIndex : sourceIndex + concreteIndexOffset;
      return `${targetIndex}${restSuffix}`;
    };
    collectAstRestCarrierAliasCopies(
      sourceTarget,
      targetAliasName,
      aliases,
      identifierBindings,
      (suffix, sourceAlias) => {
        const mappedSuffix = mapSpreadSuffix(suffix);
        if (!mappedSuffix) {
          return;
        }
        visit(mappedSuffix, sourceAlias, sourceTarget);
      }
    );
    collectAstStaticCarrierMemberSourceTargets(element.argument, (suffix, spreadSourceTarget) => {
      const mappedSuffix = mapSpreadSuffix(suffix);
      if (!mappedSuffix) {
        return;
      }
      visit(mappedSuffix, void 0, spreadSourceTarget);
    });
    const spreadLength = getAstStaticArrayElementCount(element.argument);
    if (!hasUnboundedSpread && typeof spreadLength === "number") {
      concreteIndexOffset += spreadLength;
      continue;
    }
    hasUnboundedSpread = true;
  }
}
function collectAstStaticCarrierMemberSourceTargets(node, visit) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return;
  }
  if (unwrapped.type === "ObjectExpression") {
    const seenMembers = /* @__PURE__ */ new Set();
    for (let index = (unwrapped.properties || []).length - 1; index >= 0; index -= 1) {
      const property = unwrapped.properties[index];
      if (!property) {
        continue;
      }
      if (property.type === "SpreadElement") {
        collectAstStaticCarrierMemberSourceTargets(property.argument, (member2, sourceTarget) => {
          if (seenMembers.has(member2)) {
            return;
          }
          seenMembers.add(member2);
          visit(member2, sourceTarget);
        });
        continue;
      }
      if (property.type !== "Property") {
        break;
      }
      const member = getAstStaticMemberKey(property);
      if (!member) {
        break;
      }
      if (seenMembers.has(member)) {
        continue;
      }
      seenMembers.add(member);
      visit(member, createAstPatternSourceTarget(property.value));
    }
    return;
  }
  if (unwrapped.type === "ArrayExpression") {
    let targetIndex = 0;
    for (const element of unwrapped.elements || []) {
      if (!element) {
        targetIndex += 1;
        continue;
      }
      if (element.type !== "SpreadElement") {
        visit(String(targetIndex), createAstPatternSourceTarget(element));
        targetIndex += 1;
        continue;
      }
      const spreadLength = getAstStaticArrayElementCount(element.argument);
      if (typeof spreadLength !== "number") {
        return;
      }
      collectAstStaticCarrierMemberSourceTargets(element.argument, (member, sourceTarget) => {
        const sourceIndex = Number(member);
        if (!Number.isInteger(sourceIndex) || sourceIndex < 0) {
          return;
        }
        visit(String(targetIndex + sourceIndex), sourceTarget);
      });
      targetIndex += spreadLength;
    }
  }
}
function getAstStaticArrayElementCount(node) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if ((unwrapped == null ? void 0 : unwrapped.type) !== "ArrayExpression") {
    return void 0;
  }
  let count = 0;
  for (const element of unwrapped.elements || []) {
    if (!element) {
      count += 1;
      continue;
    }
    if (element.type !== "SpreadElement") {
      count += 1;
      continue;
    }
    const spreadLength = getAstStaticArrayElementCount(element.argument);
    if (typeof spreadLength !== "number") {
      return void 0;
    }
    count += spreadLength;
  }
  return count;
}
function collectAstObjectPatternStaticMembers(pattern) {
  const members = /* @__PURE__ */ new Set();
  for (const property of (pattern == null ? void 0 : pattern.properties) || []) {
    if (!property || property.type !== "Property") {
      continue;
    }
    const member = getAstStaticPropertyName(property);
    if (member) {
      members.add(member);
    }
  }
  return members;
}
function getAstPatternTargetAliasLookup(target) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(target);
  if ((unwrapped == null ? void 0 : unwrapped.type) === "Identifier") {
    return {
      aliasName: unwrapped.name,
      index: typeof unwrapped.start === "number" ? unwrapped.start : 0,
      rootName: unwrapped.name
    };
  }
  return getAstMemberAliasLookup(unwrapped);
}
function collectAstPatternMemberExpressions(pattern, visit) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(pattern);
  if (!unwrapped) {
    return;
  }
  if (unwrapped.type === "MemberExpression") {
    visit(unwrapped);
    return;
  }
  if (unwrapped.type === "AssignmentPattern") {
    collectAstPatternMemberExpressions(unwrapped.left, visit);
    return;
  }
  if (unwrapped.type === "RestElement") {
    collectAstPatternMemberExpressions(unwrapped.argument, visit);
    return;
  }
  if (unwrapped.type === "ObjectPattern") {
    for (const property of unwrapped.properties || []) {
      if (!property) {
        continue;
      }
      collectAstPatternMemberExpressions(property.type === "Property" ? property.value : property.argument, visit);
    }
    return;
  }
  if (unwrapped.type === "ArrayPattern") {
    for (const element of unwrapped.elements || []) {
      collectAstPatternMemberExpressions(element, visit);
    }
  }
}
function createAstPatternSourceTarget(node) {
  const sourceRoot = getAstPatternSourceRoot(node);
  return {
    node,
    aliasName: sourceRoot == null ? void 0 : sourceRoot.aliasName,
    rootName: sourceRoot == null ? void 0 : sourceRoot.rootName,
    index: (sourceRoot == null ? void 0 : sourceRoot.index) ?? (typeof (node == null ? void 0 : node.start) === "number" ? node.start : 0)
  };
}
function createMaybeAstPatternSourceTarget(node) {
  return node ? createAstPatternSourceTarget(node) : void 0;
}
function getAstPatternMemberSourceTarget(sourceTarget, member) {
  const memberSourceTarget = getAstStaticCarrierMemberSourceTarget(sourceTarget == null ? void 0 : sourceTarget.node, member);
  if (memberSourceTarget) {
    return memberSourceTarget;
  }
  const sourceRoot = (sourceTarget == null ? void 0 : sourceTarget.aliasName) ? sourceTarget : getAstPatternSourceRoot(sourceTarget == null ? void 0 : sourceTarget.node);
  if (sourceRoot == null ? void 0 : sourceRoot.aliasName) {
    return {
      aliasName: `${sourceRoot.aliasName}.${member}`,
      rootName: sourceRoot.rootName,
      index: sourceRoot.index ?? (sourceTarget == null ? void 0 : sourceTarget.index) ?? 0
    };
  }
  return { index: (sourceTarget == null ? void 0 : sourceTarget.index) ?? 0 };
}
function getAstStaticCarrierMemberSourceTarget(node, member) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return void 0;
  }
  if (unwrapped.type === "ObjectExpression") {
    return getAstObjectCarrierMemberSourceTarget(unwrapped, member);
  }
  if (unwrapped.type === "ArrayExpression") {
    return getAstArrayCarrierMemberSourceTarget(unwrapped, member);
  }
  return void 0;
}
function getAstObjectCarrierMemberSourceTarget(node, member) {
  var _a;
  const fallbackSourceTargets = [];
  for (let index = (node.properties || []).length - 1; index >= 0; index -= 1) {
    const property = node.properties[index];
    if (!property) {
      continue;
    }
    if (property.type === "SpreadElement") {
      const spreadTarget = getAstSpreadMemberSourceTarget(property.argument, member);
      if (spreadTarget) {
        fallbackSourceTargets.push(spreadTarget);
      }
      continue;
    }
    if (property.type !== "Property") {
      return createAstFallbackSourceTarget(fallbackSourceTargets, property.start || node.start || 0);
    }
    const propertyKey = getAstStaticMemberKey(property);
    if (!propertyKey) {
      fallbackSourceTargets.push(createAstAmbiguousSourceTarget(createAstPatternSourceTarget(property.value)));
      continue;
    }
    if (propertyKey !== member) {
      continue;
    }
    const propertyTarget = createAstPatternSourceTarget(property.value);
    if (!fallbackSourceTargets.length) {
      return propertyTarget;
    }
    return createAstFallbackSourceTarget(
      [...fallbackSourceTargets, propertyTarget],
      typeof ((_a = property.value) == null ? void 0 : _a.start) === "number" ? property.value.start : property.start || node.start || 0
    );
  }
  return createAstFallbackSourceTarget(fallbackSourceTargets, node.start || 0);
}
function getAstArrayCarrierMemberSourceTarget(node, member) {
  const targetIndex = Number(member);
  if (!Number.isInteger(targetIndex) || targetIndex < 0) {
    return void 0;
  }
  const fallbackSourceTargets = [];
  let concreteIndex = 0;
  let hasSpreadBefore = false;
  for (const element of node.elements || []) {
    if (!element) {
      if (!hasSpreadBefore && concreteIndex === targetIndex && !fallbackSourceTargets.length) {
        return void 0;
      }
      concreteIndex += 1;
      continue;
    }
    if (element.type === "SpreadElement") {
      const spreadLength = getAstStaticArrayElementCount(element.argument);
      if (!hasSpreadBefore && typeof spreadLength === "number") {
        const spreadIndex = targetIndex - concreteIndex;
        if (spreadIndex < 0) {
          return createAstFallbackSourceTarget(fallbackSourceTargets, node.start || 0);
        }
        if (spreadIndex < spreadLength) {
          return getAstSpreadMemberSourceTarget(element.argument, String(spreadIndex));
        }
        concreteIndex += spreadLength;
        continue;
      }
      const spreadMember = hasSpreadBefore ? String(targetIndex) : String(targetIndex - concreteIndex);
      if (targetIndex >= concreteIndex || hasSpreadBefore) {
        const spreadTarget = getAstSpreadMemberSourceTarget(element.argument, spreadMember);
        if (spreadTarget) {
          fallbackSourceTargets.push(hasSpreadBefore ? createAstAmbiguousSourceTarget(spreadTarget) : spreadTarget);
        }
      }
      hasSpreadBefore = true;
      continue;
    }
    if (!hasSpreadBefore && concreteIndex === targetIndex) {
      const elementTarget = createAstPatternSourceTarget(element);
      if (!fallbackSourceTargets.length) {
        return elementTarget;
      }
      return createAstFallbackSourceTarget(
        [...fallbackSourceTargets, elementTarget],
        typeof element.start === "number" ? element.start : node.start || 0
      );
    }
    if (hasSpreadBefore) {
      fallbackSourceTargets.push(createAstAmbiguousSourceTarget(createAstPatternSourceTarget(element)));
    }
    concreteIndex += 1;
  }
  return createAstFallbackSourceTarget(fallbackSourceTargets, node.start || 0);
}
function createAstAmbiguousSourceTarget(sourceTarget) {
  return sourceTarget ? { ...sourceTarget, ambiguous: true } : void 0;
}
function getAstSpreadMemberSourceTarget(spreadArgument, member) {
  const sourceRoot = getAstPatternSourceRoot(spreadArgument);
  if (sourceRoot == null ? void 0 : sourceRoot.aliasName) {
    return {
      aliasName: `${sourceRoot.aliasName}.${member}`,
      rootName: sourceRoot.rootName,
      index: sourceRoot.index ?? (typeof (spreadArgument == null ? void 0 : spreadArgument.start) === "number" ? spreadArgument.start : 0)
    };
  }
  return getAstStaticCarrierMemberSourceTarget(spreadArgument, member);
}
function createAstFallbackSourceTarget(fallbackSourceTargets, index) {
  const targets = fallbackSourceTargets.filter(Boolean);
  return targets.length ? {
    fallbackSourceTargets: targets,
    index
  } : void 0;
}
function getAstPatternSourceRoot(node) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if ((unwrapped == null ? void 0 : unwrapped.type) === "Identifier") {
    return {
      aliasName: unwrapped.name,
      index: typeof unwrapped.start === "number" ? unwrapped.start : 0,
      rootName: unwrapped.name
    };
  }
  return getAstMemberAliasLookup(unwrapped);
}
function getAstStaticCarrierMemberValue(node, member) {
  var _a;
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return void 0;
  }
  if (unwrapped.type === "ObjectExpression") {
    for (let index = (unwrapped.properties || []).length - 1; index >= 0; index -= 1) {
      const property = unwrapped.properties[index];
      if (!property) {
        continue;
      }
      if (property.type !== "Property") {
        return void 0;
      }
      const propertyKey = getAstStaticMemberKey(property);
      if (!propertyKey) {
        return void 0;
      }
      if (propertyKey === member) {
        return property.value;
      }
    }
  }
  if (unwrapped.type === "ArrayExpression") {
    const index = Number(member);
    if (Number.isInteger(index) && index >= 0) {
      const element = (_a = unwrapped.elements) == null ? void 0 : _a[index];
      return element && element.type !== "SpreadElement" ? element : void 0;
    }
  }
  return void 0;
}
function collectAstKnownCarrierMembers(node, visit) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return;
  }
  if (unwrapped.type === "ObjectExpression") {
    const seenMembers = /* @__PURE__ */ new Set();
    for (let index = (unwrapped.properties || []).length - 1; index >= 0; index -= 1) {
      const property = unwrapped.properties[index];
      if (!property) {
        continue;
      }
      if (property.type !== "Property") {
        return;
      }
      const member = getAstStaticMemberKey(property);
      if (!member) {
        return;
      }
      if (seenMembers.has(member)) {
        continue;
      }
      seenMembers.add(member);
      visit(member, property.value, property.value || property);
    }
    return;
  }
  if (unwrapped.type === "ArrayExpression") {
    for (let index = 0; index < (unwrapped.elements || []).length; index += 1) {
      const element = unwrapped.elements[index];
      if (!element || element.type === "SpreadElement") {
        return;
      }
      visit(String(index), element, element);
    }
  }
}
function collectAstStaticCarrierMembers(node, visit) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return;
  }
  if (unwrapped.type === "ObjectExpression") {
    for (const property of unwrapped.properties || []) {
      if (!property || property.type !== "Property") {
        continue;
      }
      const member = getAstStaticMemberKey(property);
      if (member) {
        visit(member, property.value, property.value || property);
      }
    }
    return;
  }
  if (unwrapped.type === "ArrayExpression") {
    for (let index = 0; index < (unwrapped.elements || []).length; index += 1) {
      const element = unwrapped.elements[index];
      if (!element || element.type === "SpreadElement") {
        continue;
      }
      visit(String(index), element, element);
    }
  }
}
function getAstBindingIdentifierNode(node) {
  var _a;
  if ((node == null ? void 0 : node.type) === "Identifier") {
    return node;
  }
  if ((node == null ? void 0 : node.type) === "AssignmentPattern" && ((_a = node.left) == null ? void 0 : _a.type) === "Identifier") {
    return node.left;
  }
  return void 0;
}
function getAstBindingIdentifierName(node) {
  var _a;
  if ((node == null ? void 0 : node.type) === "Identifier") {
    return node.name;
  }
  if ((node == null ? void 0 : node.type) === "AssignmentPattern" && ((_a = node.left) == null ? void 0 : _a.type) === "Identifier") {
    return node.left.name;
  }
  return "";
}
function getAstObjectPatternFromValue(node) {
  var _a;
  if ((node == null ? void 0 : node.type) === "ObjectPattern") {
    return node;
  }
  if ((node == null ? void 0 : node.type) === "AssignmentPattern" && ((_a = node.left) == null ? void 0 : _a.type) === "ObjectPattern") {
    return node.left;
  }
  return void 0;
}
function trimAstAliasesAfterWrites(aliases, writes, identifierBindings) {
  return aliases.map((alias) => {
    const aliasDeclarationStart = alias.declarationStart ?? alias.start;
    const aliasRootName = getAstAliasRootName(alias.name);
    const nextWrite = writes.filter(
      (write) => (write.name === alias.name || alias.name.includes(".") && write.name === aliasRootName) && write.index > aliasDeclarationStart && write.index >= alias.start && write.index < alias.end && write.alwaysRunsInExecutionScope && (0, import_bindings.isSameAstRange)(write.executionScope, alias.executionScope) && !hasAstShadowBinding(aliasRootName, write.index, alias, identifierBindings)
    ).sort((left, right) => left.index - right.index)[0];
    return nextWrite ? { ...alias, end: nextWrite.index } : alias;
  });
}
function resolveRunJsStaticString(node, source, stringBindings, identifierBindings) {
  if (!node) {
    return void 0;
  }
  const resolved = resolveAstResourceTypeExpression(node, source, stringBindings, identifierBindings);
  return resolved.status === "resolved" ? resolved.value : void 0;
}
function resolveRunJsStaticObjectExpression(node, identifierBindings, staticFilterValueBindings) {
  return resolveRunJsStaticObjectExpressionRec(node, identifierBindings, staticFilterValueBindings);
}
function resolveRunJsStaticObjectExpressionRec(node, identifierBindings, staticFilterValueBindings, seen = /* @__PURE__ */ new Set()) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return void 0;
  }
  if (unwrapped.type === "ObjectExpression") {
    return unwrapped;
  }
  if (unwrapped.type === "Identifier") {
    const binding = resolveAstAliasBinding(
      unwrapped.name,
      unwrapped.start || 0,
      staticFilterValueBindings,
      identifierBindings
    );
    if (!binding || seen.has(binding)) {
      return void 0;
    }
    const nextSeen = new Set(seen);
    nextSeen.add(binding);
    return resolveRunJsStaticObjectExpressionRec(
      binding.valueNode,
      identifierBindings,
      staticFilterValueBindings,
      nextSeen
    );
  }
  if (unwrapped.type === "MemberExpression") {
    const object = resolveRunJsStaticObjectExpressionRec(
      unwrapped.object,
      identifierBindings,
      staticFilterValueBindings,
      seen
    );
    if (!object) {
      return void 0;
    }
    const memberName = getAstStaticPropertyName(unwrapped);
    if (!memberName) {
      return void 0;
    }
    const property = getRunJsObjectProperty(object, [memberName], identifierBindings, staticFilterValueBindings);
    return resolveRunJsStaticObjectExpressionRec(property == null ? void 0 : property.value, identifierBindings, staticFilterValueBindings, seen);
  }
  return void 0;
}
function getRunJsObjectProperty(node, names, identifierBindings, staticFilterValueBindings) {
  const lookup = getRunJsObjectPropertyLookup(node, names, identifierBindings, staticFilterValueBindings);
  return lookup.status === "found" ? lookup.property : void 0;
}
function getRunJsObjectPropertyByPath(node, members, identifierBindings, staticFilterValueBindings) {
  let current = (0, import_bindings.unwrapAstChainExpression)(node);
  let property;
  for (let index = 0; index < members.length; index += 1) {
    const member = members[index];
    property = getRunJsObjectProperty(current, [member], identifierBindings, staticFilterValueBindings);
    if (!property) {
      return void 0;
    }
    current = resolveRunJsStaticObjectExpression(
      property.value,
      identifierBindings || [],
      staticFilterValueBindings || []
    );
    if (!current && index < members.length - 1) {
      return void 0;
    }
  }
  return property;
}
function getRunJsObjectPropertyLookup(node, names, identifierBindings, staticFilterValueBindings) {
  const object = (0, import_bindings.unwrapAstChainExpression)(node);
  if ((object == null ? void 0 : object.type) !== "ObjectExpression") {
    return { status: "missing" };
  }
  const nameSet = new Set(names.map((name) => name.toLowerCase()));
  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") {
      if (identifierBindings && staticFilterValueBindings) {
        const spreadObject = resolveRunJsStaticObjectExpression(
          property.argument,
          identifierBindings,
          staticFilterValueBindings
        );
        if (spreadObject) {
          const spreadLookup = getRunJsObjectPropertyLookup(
            spreadObject,
            names,
            identifierBindings,
            staticFilterValueBindings
          );
          if (spreadLookup.status !== "missing") {
            return spreadLookup;
          }
          continue;
        }
      }
      return { status: "unknown" };
    }
    if (property.type !== "Property") {
      return { status: "unknown" };
    }
    const propertyName = (0, import_surface.normalizeText)(getAstStaticPropertyName(property));
    if (!propertyName) {
      return { status: "unknown" };
    }
    if (nameSet.has(propertyName.toLowerCase())) {
      return { status: "found", property };
    }
  }
  return { status: "missing" };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addAstFunctionParamBindings,
  collectAstArrayRestCarrierSourceTargets,
  collectAstArraySpreadCarrierCopies,
  collectAstCarrierCandidateMembers,
  collectAstCarrierSourceTargets,
  collectAstDynamicCarrierSourceTargets,
  collectAstKnownCarrierMembers,
  collectAstLiteralSpreadCarrierCopies,
  collectAstObjectPatternAliases,
  collectAstObjectPatternPathAliases,
  collectAstObjectPatternStaticMembers,
  collectAstObjectRestCarrierSourceTargets,
  collectAstObjectSpreadCarrierCopies,
  collectAstPatternBindingIdentifiers,
  collectAstPatternCarrierSourceTargets,
  collectAstPatternCarrierSourceTargetsFromSourceTarget,
  collectAstPatternDefaultValueAliases,
  collectAstPatternDefaultValueAliasesWithSource,
  collectAstPatternMemberExpressions,
  collectAstPatternRestCarrierSourceTargets,
  collectAstPatternRestCarrierSourceTargetsRec,
  collectAstPatternSourceTargets,
  collectAstPatternSourceTargetsRec,
  collectAstRestCarrierAliasCopies,
  collectAstStaticCarrierMemberSourceTargets,
  collectAstStaticCarrierMembers,
  compareAstAliasPrecedence,
  createAstAmbiguousSourceTarget,
  createAstFallbackSourceTarget,
  createAstPatternSourceTarget,
  createMaybeAstPatternSourceTarget,
  dedupeAstResourceEntries,
  findUnboundCtxMatches,
  getAstAliasPrecedenceStart,
  getAstAliasRootName,
  getAstArrayCarrierMemberSourceTarget,
  getAstAssignmentTargetScope,
  getAstBindingIdentifierName,
  getAstBindingIdentifierNode,
  getAstMemberAliasLookup,
  getAstMemberAssignmentTargetScope,
  getAstMemberRootIdentifier,
  getAstMemberWriteLookup,
  getAstObjectCarrierMemberSourceTarget,
  getAstObjectPatternFromValue,
  getAstPatternMemberSourceTarget,
  getAstPatternSourceRoot,
  getAstPatternTargetAliasLookup,
  getAstSpreadMemberSourceTarget,
  getAstStaticArrayElementCount,
  getAstStaticCarrierMemberSourceTarget,
  getAstStaticCarrierMemberValue,
  getAstStaticMemberKey,
  getAstStaticPropertyName,
  getCtxMethodName,
  getRunJsObjectProperty,
  getRunJsObjectPropertyByPath,
  getRunJsObjectPropertyLookup,
  hasAstActiveBinding,
  hasAstShadowBinding,
  isAstCtxApiAliasAssignmentOperator,
  isAstDefiniteAssignmentOperator,
  isAstDynamicMemberAliasMatch,
  isCtxIdentifier,
  isCtxRootFromAst,
  isUnshadowedCtxIdentifier,
  resolveAstActiveIdentifierBinding,
  resolveAstAliasBinding,
  resolveAstDynamicAliasBinding,
  resolveAstMemberAliasBinding,
  resolveAstNamedAliasBinding,
  resolveAstResourceTypeExpression,
  resolveAstStaticStringValue,
  resolveAstStaticTemplateLiteralValue,
  resolveCtxMethodCall,
  resolveRunJsStaticObjectExpression,
  resolveRunJsStaticObjectExpressionRec,
  resolveRunJsStaticString,
  trimAstAliasesAfterWrites
});
