"use strict";
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 logicalNameResolver_exports = {};
__export(logicalNameResolver_exports, {
  resolveCollisions: () => resolveCollisions
});
module.exports = __toCommonJS(logicalNameResolver_exports);
var import_slug = require("./slug");
function resolveCollisions(inputs) {
  const groupsByKey = /* @__PURE__ */ new Map();
  inputs.forEach((inp, idx) => {
    const key = `${inp.sectionName}::${inp.initialName}`;
    if (!groupsByKey.has(key)) groupsByKey.set(key, []);
    groupsByKey.get(key).push(idx);
  });
  const output = new Array(inputs.length);
  const slugOrNull = (s) => {
    if (!s) return null;
    const slugged = (0, import_slug.slug)(s);
    return slugged ? slugged : null;
  };
  for (const [, indices] of groupsByKey.entries()) {
    if (indices.length === 1) {
      output[indices[0]] = inputs[indices[0]].initialName;
      continue;
    }
    const candidateNames = indices.map((i) => {
      const inp = inputs[i];
      const testIdSlug = slugOrNull(inp.testId);
      if (testIdSlug) return `${testIdSlug}_${inp.initialName}`;
      const stableIdSlug = slugOrNull(inp.stableId);
      if (stableIdSlug) return `${stableIdSlug}_${inp.initialName}`;
      const ancestorSlug = slugOrNull(inp.nearestAncestorId);
      if (ancestorSlug) return `${ancestorSlug}_${inp.initialName}`;
      return null;
    });
    const nonNull = candidateNames.filter((n) => n !== null);
    const hasDuplicates = new Set(nonNull).size !== nonNull.length;
    const anyNull = candidateNames.some((n) => n === null);
    if (anyNull || hasDuplicates) {
      indices.forEach((idx, posWithinGroup) => {
        const inp = inputs[idx];
        output[idx] = `${inp.sectionName}_${inp.initialName}_${posWithinGroup + 1}`;
      });
    } else {
      indices.forEach((idx, k) => {
        output[idx] = candidateNames[k];
      });
    }
  }
  return output;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  resolveCollisions
});
