/**
 * 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_bindings_exports = {};
__export(static_bindings_exports, {
  collectAstFunctionBindingsFromAst: () => collectAstFunctionBindingsFromAst,
  collectAstIdentifierBindingsFromAst: () => collectAstIdentifierBindingsFromAst,
  collectAstStaticAliasCopyWrites: () => collectAstStaticAliasCopyWrites,
  collectAstStaticBindingWritesFromAst: () => collectAstStaticBindingWritesFromAst,
  collectAstStaticObjectAliasCopiesFromAst: () => collectAstStaticObjectAliasCopiesFromAst,
  collectStaticFilterValueBindingsFromAst: () => collectStaticFilterValueBindingsFromAst,
  collectStaticStringBindingsFromAst: () => collectStaticStringBindingsFromAst,
  getAstIdentifierWriteKey: () => getAstIdentifierWriteKey,
  isAstStaticObjectAliasCopyActiveAtIndex: () => isAstStaticObjectAliasCopyActiveAtIndex,
  isAstStaticObjectAliasCopySourceActiveAtIndex: () => isAstStaticObjectAliasCopySourceActiveAtIndex,
  isStaticStringBindingActiveAtIndex: () => isStaticStringBindingActiveAtIndex,
  resolveAstStaticAliasCopySourceName: () => resolveAstStaticAliasCopySourceName,
  trimStaticFilterValueBindingsAfterWrites: () => trimStaticFilterValueBindingsAfterWrites,
  trimStaticStringBindingsAfterWrites: () => trimStaticStringBindingsAfterWrites
});
module.exports = __toCommonJS(static_bindings_exports);
var import_walk = require("./walk");
var import_bindings = require("./bindings");
var import_execution = require("./execution");
var import_static_values = require("./static-values");
function collectAstIdentifierBindingsFromAst(ast, source) {
  const bindings = [];
  const addBinding = (name, node, scope, declarationStart, unavailableRanges) => {
    if (!name) {
      return;
    }
    bindings.push({
      name,
      declarationStart: typeof declarationStart === "number" ? declarationStart : typeof (node == null ? void 0 : node.start) === "number" ? node.start : scope.start,
      unavailableRanges,
      start: scope.start,
      end: scope.end
    });
  };
  (0, import_walk.walkAstAncestor)(ast, {
    VariableDeclarator(node, ancestors) {
      var _a;
      const declaration = (0, import_bindings.findAstAncestor)(ancestors, "VariableDeclaration");
      const scope = (0, import_bindings.getAstBindingScopeRange)(ancestors, source.length, (declaration == null ? void 0 : declaration.kind) === "var");
      const unavailableRanges = (declaration == null ? void 0 : declaration.kind) === "var" ? void 0 : getForHeadUnavailableBindingRanges(declaration, ancestors);
      if (((_a = node.id) == null ? void 0 : _a.type) === "Identifier") {
        const declarationStart = (declaration == null ? void 0 : declaration.kind) === "var" ? scope.start : typeof node.end === "number" ? node.end : typeof node.start === "number" ? node.start : scope.start;
        addBinding(node.id.name, node.id, scope, declarationStart, unavailableRanges);
        return;
      }
      collectAstPatternBindingDeclarations(
        node.id,
        (name, bindingNode, declarationStart) => addBinding(
          name,
          bindingNode,
          scope,
          (declaration == null ? void 0 : declaration.kind) === "var" ? scope.start : declarationStart,
          unavailableRanges
        )
      );
    },
    FunctionDeclaration(node, ancestors) {
      var _a;
      const parentScope = (0, import_bindings.getAstBindingScopeRange)(ancestors.slice(0, -1), source.length);
      if (((_a = node.id) == null ? void 0 : _a.type) === "Identifier") {
        addBinding(node.id.name, node.id, parentScope, parentScope.start);
      }
      (0, import_static_values.addAstFunctionParamBindings)(bindings, node, source.length);
    },
    FunctionExpression(node) {
      var _a;
      if (((_a = node.id) == null ? void 0 : _a.type) === "Identifier") {
        bindings.push({
          name: node.id.name,
          start: typeof node.id.start === "number" ? node.id.start : typeof node.start === "number" ? node.start : 0,
          end: typeof node.end === "number" ? node.end : source.length
        });
      }
      (0, import_static_values.addAstFunctionParamBindings)(bindings, node, source.length);
    },
    ArrowFunctionExpression(node) {
      (0, import_static_values.addAstFunctionParamBindings)(bindings, node, source.length);
    },
    ClassDeclaration(node, ancestors) {
      var _a;
      const parentScope = (0, import_bindings.getAstBindingScopeRange)(ancestors.slice(0, -1), source.length);
      if (((_a = node.id) == null ? void 0 : _a.type) === "Identifier") {
        addBinding(node.id.name, node.id, parentScope);
      }
    },
    ClassExpression(node) {
      var _a;
      if (((_a = node.id) == null ? void 0 : _a.type) === "Identifier") {
        bindings.push({
          name: node.id.name,
          start: typeof node.id.start === "number" ? node.id.start : typeof node.start === "number" ? node.start : 0,
          end: typeof node.end === "number" ? node.end : source.length
        });
      }
    },
    CatchClause(node) {
      const scope = {
        start: typeof node.start === "number" ? node.start : 0,
        end: typeof node.end === "number" ? node.end : source.length
      };
      (0, import_static_values.collectAstPatternBindingIdentifiers)(node.param, (name, bindingNode) => addBinding(name, bindingNode, scope));
    }
  });
  return bindings;
}
function getForHeadUnavailableBindingRanges(declaration, ancestors) {
  var _a, _b;
  const parent = ancestors[ancestors.length - 3];
  if ((parent == null ? void 0 : parent.type) !== "ForOfStatement" && (parent == null ? void 0 : parent.type) !== "ForInStatement" || parent.left !== declaration || typeof ((_a = parent.right) == null ? void 0 : _a.start) !== "number" || typeof ((_b = parent.right) == null ? void 0 : _b.end) !== "number") {
    return void 0;
  }
  return [
    {
      start: parent.right.start,
      end: parent.right.end
    }
  ];
}
function collectAstPatternBindingDeclarations(node, addBinding, declarationStart) {
  if (!node) {
    return;
  }
  const fallbackStart = typeof declarationStart === "number" ? declarationStart : typeof node.end === "number" ? node.end : typeof node.start === "number" ? node.start : 0;
  if (node.type === "Identifier") {
    addBinding(node.name, node, fallbackStart);
    return;
  }
  if (node.type === "AssignmentPattern") {
    collectAstPatternBindingDeclarations(
      node.left,
      addBinding,
      typeof node.end === "number" ? node.end : fallbackStart
    );
    return;
  }
  if (node.type === "RestElement") {
    collectAstPatternBindingDeclarations(
      node.argument,
      addBinding,
      typeof node.end === "number" ? node.end : fallbackStart
    );
    return;
  }
  if (node.type === "ArrayPattern") {
    for (const element of node.elements || []) {
      collectAstPatternBindingDeclarations(element, addBinding);
    }
    return;
  }
  if (node.type === "ObjectPattern") {
    for (const property of node.properties || []) {
      if (!property) {
        continue;
      }
      if (property.type === "RestElement") {
        collectAstPatternBindingDeclarations(
          property.argument,
          addBinding,
          typeof property.end === "number" ? property.end : void 0
        );
        continue;
      }
      if (property.type === "Property") {
        collectAstPatternBindingDeclarations(property.value, addBinding);
      }
    }
  }
}
function collectAstFunctionBindingsFromAst(ast, source) {
  const bindings = [];
  const addBinding = (name, functionNode, scope, declarationNode, bindingStart = scope.start, hoisted = false) => {
    if (!name || !(0, import_bindings.isAstFunctionLike)(functionNode)) {
      return;
    }
    bindings.push({
      name,
      functionNode,
      declarationStart: typeof (declarationNode == null ? void 0 : declarationNode.start) === "number" ? declarationNode.start : typeof functionNode.start === "number" ? functionNode.start : scope.start,
      start: bindingStart,
      end: scope.end,
      hoisted,
      scopeStart: scope.start
    });
  };
  (0, import_walk.walkAstAncestor)(ast, {
    FunctionDeclaration(node, ancestors) {
      var _a;
      const parentScope = (0, import_bindings.getAstBindingScopeRange)(ancestors.slice(0, -1), source.length);
      if (((_a = node.id) == null ? void 0 : _a.type) === "Identifier") {
        addBinding(node.id.name, node, parentScope, node.id, parentScope.start, true);
      }
    },
    VariableDeclarator(node, ancestors) {
      var _a;
      if (((_a = node.id) == null ? void 0 : _a.type) !== "Identifier") {
        return;
      }
      const functionNode = (0, import_bindings.unwrapAstChainExpression)(node.init);
      if (!(0, import_bindings.isAstFunctionLike)(functionNode)) {
        return;
      }
      const declaration = (0, import_bindings.findAstAncestor)(ancestors, "VariableDeclaration");
      const scope = (0, import_bindings.getAstBindingScopeRange)(ancestors, source.length, (declaration == null ? void 0 : declaration.kind) === "var");
      addBinding(
        node.id.name,
        functionNode,
        scope,
        node.id,
        typeof node.id.start === "number" ? node.id.start : scope.start
      );
    },
    AssignmentExpression(node, ancestors) {
      var _a;
      if (node.operator !== "=" || ((_a = node.left) == null ? void 0 : _a.type) !== "Identifier") {
        return;
      }
      const functionNode = (0, import_bindings.unwrapAstChainExpression)(node.right);
      if (!(0, import_bindings.isAstFunctionLike)(functionNode)) {
        return;
      }
      const scope = (0, import_bindings.getAstExecutionScopeRange)(ancestors, source.length);
      addBinding(
        node.left.name,
        functionNode,
        scope,
        node.left,
        typeof node.left.start === "number" ? node.left.start : scope.start
      );
    }
  });
  return bindings;
}
function collectStaticStringBindingsFromAst(ast, source, seedStringBindings = [], identifierBindings = []) {
  const bindings = [];
  const availableBindings = [...seedStringBindings];
  const writes = collectAstStaticBindingWritesFromAst(ast, source, identifierBindings);
  const aliasCopies = collectAstStaticObjectAliasCopiesFromAst(ast, source, identifierBindings);
  const addBinding = (name, value, node, scope, declarationStart, executionScope) => {
    const binding = {
      declarationStart,
      executionScope,
      name,
      value,
      start: typeof (node == null ? void 0 : node.start) === "number" ? node.start : declarationStart,
      end: scope.end
    };
    bindings.push(binding);
    availableBindings.push(binding);
  };
  const copyMemberBindings = (sourceName, targetName, node, scope, declarationStart, executionScope) => {
    const exactBinding = availableBindings.find(
      (binding) => binding.name === sourceName && isStaticStringBindingActiveAtIndex(binding, declarationStart, writes, identifierBindings)
    );
    if (exactBinding && !availableBindings.some((binding) => binding.name === targetName && binding.start >= declarationStart)) {
      addBinding(targetName, exactBinding.value, node, scope, declarationStart, executionScope);
    }
    const prefix = `${sourceName}.`;
    const sourceBindings = availableBindings.filter(
      (binding) => binding.name.startsWith(prefix) && isStaticStringBindingActiveAtIndex(binding, declarationStart, writes, identifierBindings)
    );
    for (const sourceBinding of sourceBindings) {
      const name = `${targetName}${sourceBinding.name.slice(sourceName.length)}`;
      if (availableBindings.some((binding) => binding.name === name && binding.start >= declarationStart)) {
        continue;
      }
      addBinding(name, sourceBinding.value, node, scope, declarationStart, executionScope);
    }
  };
  const collectMemberBindings = (prefix, valueNode, scope, declarationStart, executionScope, bindingStartNode) => {
    const unwrapped = (0, import_bindings.unwrapAstChainExpression)(valueNode);
    if ((unwrapped == null ? void 0 : unwrapped.type) !== "ObjectExpression") {
      return;
    }
    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") {
        break;
      }
      if (property.type !== "Property") {
        break;
      }
      const member = (0, import_static_values.getAstStaticMemberKey)(property);
      if (!member) {
        break;
      }
      if (seenMembers.has(member)) {
        continue;
      }
      seenMembers.add(member);
      const bindingName = `${prefix}.${member}`;
      const resolved = (0, import_static_values.resolveRunJsStaticString)(property.value, source, availableBindings, identifierBindings);
      if (typeof resolved === "string") {
        addBinding(bindingName, resolved, bindingStartNode, scope, declarationStart, executionScope);
        continue;
      }
      collectMemberBindings(bindingName, property.value, scope, declarationStart, executionScope, bindingStartNode);
    }
  };
  (0, import_walk.walkAstAncestor)(ast, {
    VariableDeclarator(node, ancestors) {
      var _a, _b;
      const declaration = (0, import_bindings.findAstAncestor)(ancestors, "VariableDeclaration");
      if ((declaration == null ? void 0 : declaration.kind) !== "const") {
        return;
      }
      const scope = (0, import_bindings.getAstBindingScopeRange)(ancestors, source.length, (declaration == null ? void 0 : declaration.kind) === "var");
      const executionScope = (0, import_bindings.getAstExecutionScopeRange)(ancestors, source.length);
      const declarationStart = typeof node.start === "number" ? node.start : scope.start;
      if (((_a = node.id) == null ? void 0 : _a.type) === "ObjectPattern") {
        const sourceName2 = resolveAstStaticAliasCopySourceName(node.init, aliasCopies, identifierBindings);
        if (!sourceName2) {
          return;
        }
        (0, import_static_values.collectAstObjectPatternPathAliases)(node.id, (name, members, aliasNode) => {
          const aliasDeclarationStart = typeof (aliasNode == null ? void 0 : aliasNode.start) === "number" ? aliasNode.start : declarationStart;
          copyMemberBindings(
            `${sourceName2}.${members.join(".")}`,
            name,
            aliasNode || node,
            scope,
            aliasDeclarationStart,
            executionScope
          );
        });
        return;
      }
      if (((_b = node.id) == null ? void 0 : _b.type) !== "Identifier") {
        return;
      }
      const resolved = (0, import_static_values.resolveRunJsStaticString)(node.init, source, availableBindings, identifierBindings);
      if (typeof resolved === "string") {
        addBinding(node.id.name, resolved, node, scope, declarationStart, executionScope);
      }
      collectMemberBindings(node.id.name, node.init, scope, declarationStart, executionScope, node);
      const sourceName = resolveAstStaticAliasCopySourceName(node.init, aliasCopies, identifierBindings);
      if (sourceName) {
        copyMemberBindings(sourceName, node.id.name, node, scope, declarationStart, executionScope);
      }
    }
  });
  return trimStaticStringBindingsAfterWrites(bindings, writes, identifierBindings);
}
function isStaticStringBindingActiveAtIndex(binding, index, writes, identifierBindings) {
  if (index < binding.start || index >= binding.end) {
    return false;
  }
  const declarationStart = binding.declarationStart ?? binding.start;
  const rootName = (0, import_static_values.getAstAliasRootName)(binding.name);
  const executionScope = binding.executionScope;
  if ((0, import_static_values.hasAstShadowBinding)(rootName, index, binding, identifierBindings)) {
    return false;
  }
  if (!executionScope) {
    return true;
  }
  return !writes.some(
    (write) => (write.name === binding.name || binding.name.startsWith(`${write.name}.`)) && write.index > declarationStart && write.index >= binding.start && write.index < index && write.alwaysRunsInExecutionScope && (0, import_bindings.isSameAstRange)(write.executionScope, executionScope) && !(0, import_static_values.hasAstShadowBinding)(rootName, write.index, binding, identifierBindings)
  );
}
function trimStaticStringBindingsAfterWrites(bindings, writes, identifierBindings) {
  return bindings.map((binding) => {
    const declarationStart = binding.declarationStart ?? binding.start;
    const rootName = (0, import_static_values.getAstAliasRootName)(binding.name);
    const executionScope = binding.executionScope;
    const nextWrite = writes.filter(
      (write) => (write.name === binding.name || binding.name.startsWith(`${write.name}.`)) && write.index > declarationStart && write.index >= binding.start && write.index < binding.end && write.alwaysRunsInExecutionScope && !!executionScope && (0, import_bindings.isSameAstRange)(write.executionScope, executionScope) && !(0, import_static_values.hasAstShadowBinding)(rootName, write.index, binding, identifierBindings)
    ).sort((left, right) => left.index - right.index)[0];
    return nextWrite ? { ...binding, end: nextWrite.index } : binding;
  });
}
function collectStaticFilterValueBindingsFromAst(ast, source, identifierBindings) {
  const bindings = [];
  const availableBindings = [];
  const writes = collectAstStaticBindingWritesFromAst(ast, source, identifierBindings);
  const getActiveBindings = (index) => trimStaticFilterValueBindingsAfterWrites(availableBindings, writes, identifierBindings).filter(
    (binding) => index >= binding.start && index < binding.end
  );
  const addBinding = (name, valueNode, node, scope, declarationStart, executionScope) => {
    const binding = {
      declarationStart,
      executionScope,
      name,
      start: typeof (node == null ? void 0 : node.end) === "number" ? node.end : declarationStart,
      end: scope.end,
      valueNode
    };
    bindings.push(binding);
    availableBindings.push(binding);
  };
  (0, import_walk.walkAstAncestor)(ast, {
    VariableDeclarator(node, ancestors) {
      var _a, _b;
      if (!node.init) {
        return;
      }
      const declaration = (0, import_bindings.findAstAncestor)(ancestors, "VariableDeclaration");
      if ((declaration == null ? void 0 : declaration.kind) !== "const") {
        return;
      }
      const scope = (0, import_bindings.getAstBindingScopeRange)(ancestors, source.length);
      const executionScope = (0, import_bindings.getAstExecutionScopeRange)(ancestors, source.length);
      const declarationStart = typeof node.start === "number" ? node.start : scope.start;
      if (((_a = node.id) == null ? void 0 : _a.type) === "ObjectPattern") {
        const sourceObject = (0, import_static_values.resolveRunJsStaticObjectExpression)(
          node.init,
          identifierBindings,
          getActiveBindings(declarationStart)
        );
        if (!sourceObject) {
          return;
        }
        (0, import_static_values.collectAstObjectPatternPathAliases)(node.id, (name, members, aliasNode) => {
          const property = (0, import_static_values.getRunJsObjectPropertyByPath)(
            sourceObject,
            members,
            identifierBindings,
            getActiveBindings(declarationStart)
          );
          if (property) {
            addBinding(
              name,
              property.value,
              aliasNode || node,
              scope,
              typeof (aliasNode == null ? void 0 : aliasNode.start) === "number" ? aliasNode.start : declarationStart,
              executionScope
            );
          }
        });
        return;
      }
      if (((_b = node.id) == null ? void 0 : _b.type) !== "Identifier") {
        return;
      }
      addBinding(node.id.name, node.init, node, scope, declarationStart, executionScope);
    }
  });
  return trimStaticFilterValueBindingsAfterWrites(bindings, writes, identifierBindings);
}
function trimStaticFilterValueBindingsAfterWrites(bindings, writes, identifierBindings) {
  return bindings.map((binding) => {
    const nextWrite = writes.filter(
      (write) => (write.name === binding.name || write.name.startsWith(`${binding.name}.`)) && write.index > binding.declarationStart && write.index >= binding.start && write.index < binding.end && write.alwaysRunsInExecutionScope && (0, import_bindings.isSameAstRange)(write.executionScope, binding.executionScope) && !(0, import_static_values.hasAstShadowBinding)(binding.name, write.index, binding, identifierBindings)
    ).sort((left, right) => left.index - right.index)[0];
    return nextWrite ? { ...binding, end: nextWrite.index } : binding;
  });
}
function collectAstStaticBindingWritesFromAst(ast, source, identifierBindings) {
  const writes = (0, import_execution.collectAstIdentifierWritesFromAst)(ast, source);
  const aliasCopies = collectAstStaticObjectAliasCopiesFromAst(ast, source, identifierBindings);
  return [...writes, ...collectAstStaticAliasCopyWrites(writes, aliasCopies, identifierBindings)];
}
function collectAstStaticObjectAliasCopiesFromAst(ast, source, identifierBindings) {
  const aliases = [];
  const resolveSourceName = (node) => resolveAstStaticAliasCopySourceName(node, aliases, identifierBindings);
  (0, import_walk.walkAstAncestor)(ast, {
    VariableDeclarator(node, ancestors) {
      var _a, _b;
      if (!node.init) {
        return;
      }
      const declaration = (0, import_bindings.findAstAncestor)(ancestors, "VariableDeclaration");
      if ((declaration == null ? void 0 : declaration.kind) !== "const") {
        return;
      }
      const scope = (0, import_bindings.getAstBindingScopeRange)(ancestors, source.length);
      const executionScope = (0, import_bindings.getAstExecutionScopeRange)(ancestors, source.length);
      const addAlias = (name, sourceName2, aliasNode) => {
        if (!sourceName2 || sourceName2 === name || sourceName2.startsWith(`${name}.`)) {
          return;
        }
        aliases.push({
          declarationStart: typeof node.start === "number" ? node.start : scope.start,
          executionScope,
          name,
          sourceName: sourceName2,
          start: typeof (aliasNode == null ? void 0 : aliasNode.start) === "number" ? aliasNode.start : typeof node.start === "number" ? node.start : scope.start,
          end: scope.end
        });
      };
      if (((_a = node.id) == null ? void 0 : _a.type) === "Identifier") {
        addAlias(node.id.name, resolveSourceName(node.init), node);
        return;
      }
      if (((_b = node.id) == null ? void 0 : _b.type) !== "ObjectPattern") {
        return;
      }
      const sourceName = resolveSourceName(node.init);
      if (!sourceName) {
        return;
      }
      (0, import_static_values.collectAstObjectPatternPathAliases)(node.id, (name, members, aliasNode) => {
        addAlias(name, `${sourceName}.${members.join(".")}`, aliasNode || node);
      });
    }
  });
  return aliases;
}
function resolveAstStaticAliasCopySourceName(node, aliases, identifierBindings) {
  const unwrapped = (0, import_bindings.unwrapAstChainExpression)(node);
  if (!unwrapped) {
    return void 0;
  }
  if (unwrapped.type === "Identifier") {
    const alias = (0, import_static_values.resolveAstAliasBinding)(unwrapped.name, unwrapped.start || 0, aliases, identifierBindings);
    return (alias == null ? void 0 : alias.sourceName) || unwrapped.name;
  }
  if (unwrapped.type !== "MemberExpression") {
    return void 0;
  }
  const lookup = (0, import_static_values.getAstMemberAliasLookup)(unwrapped);
  if (!lookup) {
    return void 0;
  }
  const rootAlias = (0, import_static_values.resolveAstAliasBinding)(lookup.rootName, lookup.index, aliases, identifierBindings);
  if (!rootAlias) {
    return lookup.aliasName;
  }
  const suffix = lookup.aliasName.slice(lookup.rootName.length);
  return `${rootAlias.sourceName}${suffix}`;
}
function collectAstStaticAliasCopyWrites(writes, aliases, identifierBindings) {
  const mirroredWrites = [];
  const seen = new Set(writes.map((write) => getAstIdentifierWriteKey(write)));
  const queue = [...writes];
  const addMirroredWrite = (write, name) => {
    if (!name || name === write.name) {
      return;
    }
    const mirroredWrite = { ...write, name };
    const key = getAstIdentifierWriteKey(mirroredWrite);
    if (seen.has(key)) {
      return;
    }
    seen.add(key);
    mirroredWrites.push(mirroredWrite);
    queue.push(mirroredWrite);
  };
  for (let index = 0; index < queue.length; index += 1) {
    const write = queue[index];
    for (const alias of aliases) {
      if (!isAstStaticObjectAliasCopyActiveAtIndex(alias, write.index, identifierBindings)) {
        continue;
      }
      if (write.name.startsWith(`${alias.name}.`)) {
        addMirroredWrite(write, `${alias.sourceName}${write.name.slice(alias.name.length)}`);
      }
      if ((write.name === alias.sourceName || write.name.startsWith(`${alias.sourceName}.`)) && isAstStaticObjectAliasCopySourceActiveAtIndex(alias, write.index, identifierBindings)) {
        addMirroredWrite(write, `${alias.name}${write.name.slice(alias.sourceName.length)}`);
      }
    }
  }
  return mirroredWrites;
}
function getAstIdentifierWriteKey(write) {
  return [
    write.name,
    write.index,
    write.alwaysRunsInExecutionScope ? "1" : "0",
    write.executionScope.start,
    write.executionScope.end
  ].join(":");
}
function isAstStaticObjectAliasCopyActiveAtIndex(alias, index, identifierBindings) {
  return index >= alias.start && index < alias.end && !(0, import_static_values.hasAstShadowBinding)((0, import_static_values.getAstAliasRootName)(alias.name), index, alias, identifierBindings);
}
function isAstStaticObjectAliasCopySourceActiveAtIndex(alias, index, identifierBindings) {
  return !(0, import_static_values.hasAstShadowBinding)((0, import_static_values.getAstAliasRootName)(alias.sourceName), index, alias, identifierBindings);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  collectAstFunctionBindingsFromAst,
  collectAstIdentifierBindingsFromAst,
  collectAstStaticAliasCopyWrites,
  collectAstStaticBindingWritesFromAst,
  collectAstStaticObjectAliasCopiesFromAst,
  collectStaticFilterValueBindingsFromAst,
  collectStaticStringBindingsFromAst,
  getAstIdentifierWriteKey,
  isAstStaticObjectAliasCopyActiveAtIndex,
  isAstStaticObjectAliasCopySourceActiveAtIndex,
  isStaticStringBindingActiveAtIndex,
  resolveAstStaticAliasCopySourceName,
  trimStaticFilterValueBindingsAfterWrites,
  trimStaticStringBindingsAfterWrites
});
