Repository: babel/babel (TypeScript)

Issue:
[Bug]: Wrong refrences of merging enum
### 💻

- [ ] Would you like to work on a fix?

### How are you using Babel?

Programmatic API (`babel.transform`, `babel.parse`)

### Input code

```TypeScript
const Cat = 10;
const Dog = 20;

enum Animals {
	Cat = 1
}

enum Animals {
	Dog = 2
}

enum Animals {
	CatDog =  Cat | Dog
}

console.log(Animals.CatDog);
```

### Configuration file name

_No response_

### Configuration

_No response_

### Current and expected behavior

```JavaScript 
const Cat = 10;
const Dog = 20;
var Animals = /*#__PURE__*/function (Animals) {
  Animals[Animals["Cat"] = 1] = "Cat";
  return Animals;
}(Animals || {});
Animals = /*#__PURE__*/function (Animals) {
  Animals[Animals["Dog"] = 2] = "Dog";
  return Animals;
}(Animals || {});
Animals = /*#__PURE__*/function (Animals) {
  Animals[Animals["CatDog"] = 30] = "CatDog";
  return Animals;
}(Animals || {});
console.log(Animals.CatDog);
```

Current:
30 is logged

Expected:
3 should be logged

### Environment

Playground:
https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=MYewdgzgLgBAwgQ1gXhgRgAwG4BQpKwAiIA5jKgEzY44CmYArgLYwCCYAlkwgDYQwBvHAEhEKdDgC-NeszadufQSOJlKUmYxbsuvfkNFJV5GPCQwAPjFUa84CCB60AdD1IAKHYojOxqgJRYQA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ctypescript&prettier=false&targets=&version=7.23.4&externalPlugins=&assumptions=%7B%7D

### Possible solution

_No response_

### Additional context

_No response_
[Bug]: References to merging enum member is lost
### 💻

- [ ] Would you like to work on a fix?

### How are you using Babel?

Programmatic API (`babel.transform`, `babel.parse`)

### Input code

```TypeScript
enum Animals {
	Cat = 1
}

enum Animals {
	Dog = 2
}

enum Animals {
	CatDog =  Cat - Dog
}
```

### Configuration file name

_No response_

### Configuration

_No response_

### Current and expected behavior

Current:

```JavaScript
var Animals = /*#__PURE__*/function (Animals) {
  Animals[Animals["Cat"] = 1] = "Cat";
  return Animals;
}(Animals || {});
Animals = /*#__PURE__*/function (Animals) {
  Animals[Animals["Dog"] = 2] = "Dog";
  return Animals;
}(Animals || {});
Animals = function (Animals) {
  Animals[Animals["CatDog"] = Animals.Cat - Dog] = "CatDog";
  return Animals;
}(Animals || {});
```

Expected: 
```JavaScript
var Animals = /*#__PURE__*/function (Animals) {
  Animals[Animals["Cat"] = 1] = "Cat";
  return Animals;
}(Animals || {});
Animals = /*#__PURE__*/function (Animals) {
  Animals[Animals["Dog"] = 2] = "Dog";
  return Animals;
}(Animals || {});
Animals = function (Animals) {
  Animals[Animals["CatDog"] = Animals.Cat - Animals.Dog] = "CatDog";
  return Animals;
}(Animals || {});
```

> [!NOTE]  
> The `Dog` should be qualified 

### Environment

Playground: 
https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=KYOwrgtgBAgiCWECGAbAzlA3gKAJAGEkAXKAXigEZsBfbbUSWBZdLPAEQHsBzMqAJhp0G0OIlQYcBYl17kohEgFoosoUA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ctypescript&prettier=false&targets=&version=7.23.4&externalPlugins=&assumptions=%7B%7D

### Possible solution

_No response_

### Additional context

_No response_


Hints:
Hey @magic-akari! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant [Slack community](https://babeljs.slack.com) that typically always has someone willing to help. You can sign-up [here](https://slack.babeljs.io/) for an invite.
Hey @magic-akari! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant [Slack community](https://babeljs.slack.com) that typically always has someone willing to help. You can sign-up [here](https://slack.babeljs.io/) for an invite.

Patch:
diff --git a/packages/babel-plugin-transform-typescript/src/enum.ts b/packages/babel-plugin-transform-typescript/src/enum.ts
--- a/packages/babel-plugin-transform-typescript/src/enum.ts
+++ b/packages/babel-plugin-transform-typescript/src/enum.ts
@@ -155,7 +155,9 @@ const enumSelfReferenceVisitor = {
 };
 
 export function translateEnumValues(path: NodePath<t.TSEnumDeclaration>, t: t) {
-  const seen: PreviousEnumMembers = new Map();
+  const bindingIdentifier = path.scope.getBindingIdentifier(path.node.id.name);
+  const seen: PreviousEnumMembers = ENUMS.get(bindingIdentifier) ?? new Map();
+
   // Start at -1 so the first enum member is its increment, 0.
   let constValue: number | string | undefined = -1;
   let lastName: string;
@@ -320,15 +322,11 @@ function computeConstantValue(
       }
 
       if (seen.has(path.node)) return;
+      seen.add(path.node);
 
-      const bindingInitPath = path.resolve(); // It only resolves constant bindings
-      if (bindingInitPath) {
-        seen.add(path.node);
-
-        value = computeConstantValue(bindingInitPath, undefined, seen);
-        prevMembers?.set(name, value);
-        return value;
-      }
+      value = computeConstantValue(path.resolve(), prevMembers, seen);
+      prevMembers?.set(name, value);
+      return value;
     }
   }
 


Test patch:
diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references-shadow/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references-shadow/input.ts
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references-shadow/input.ts
@@ -0,0 +1,14 @@
+const Cat = 10;
+const Dog = 20;
+
+enum Animals {
+	Cat = 1
+}
+
+enum Animals {
+	Dog = 2
+}
+
+enum Animals {
+	CatDog =  Cat | Dog
+}
diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references-shadow/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references-shadow/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references-shadow/output.js
@@ -0,0 +1,14 @@
+const Cat = 10;
+const Dog = 20;
+var Animals = /*#__PURE__*/function (Animals) {
+  Animals[Animals["Cat"] = 1] = "Cat";
+  return Animals;
+}(Animals || {});
+Animals = /*#__PURE__*/function (Animals) {
+  Animals[Animals["Dog"] = 2] = "Dog";
+  return Animals;
+}(Animals || {});
+Animals = /*#__PURE__*/function (Animals) {
+  Animals[Animals["CatDog"] = 3] = "CatDog";
+  return Animals;
+}(Animals || {});
diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references/input.ts
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references/input.ts
@@ -0,0 +1,7 @@
+enum Animals {
+	Cat = 1,
+	Dog = 2
+}
+enum Animals {
+	CatDog =  Cat - Dog
+}
diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/enum-merging-inner-references/output.js
@@ -0,0 +1,9 @@
+var Animals = /*#__PURE__*/function (Animals) {
+  Animals[Animals["Cat"] = 1] = "Cat";
+  Animals[Animals["Dog"] = 2] = "Dog";
+  return Animals;
+}(Animals || {});
+Animals = /*#__PURE__*/function (Animals) {
+  Animals[Animals["CatDog"] = -1] = "CatDog";
+  return Animals;
+}(Animals || {});