"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const OUT_DIR = "out";
const STAGE_DIR = "typechain-artifacts";
function main() {
    if (fs_1.default.existsSync(STAGE_DIR)) {
        fs_1.default.rmdirSync(STAGE_DIR, { recursive: true });
    }
    fs_1.default.mkdirSync(STAGE_DIR, { recursive: true });
    const files = fast_glob_1.default.sync([`${OUT_DIR}/**/*.json`, `!${OUT_DIR}/build-info/**`], {
        dot: false,
        onlyFiles: true,
    });
    const seen = new Map(); // contractName -> first file path kept
    const dups = [];
    for (const file of files) {
        const name = file.split("/").pop()?.split(".")[0];
        if (!name)
            continue;
        const already = seen.get(name);
        if (already) {
            dups.push({ name, kept: already, dropped: file });
            continue;
        }
        seen.set(name, file);
        // One artifact per name => stable TypeChain inputs
        const dest = path_1.default.join(STAGE_DIR, `${name}.json`);
        fs_1.default.copyFileSync(file, dest);
    }
    if (dups.length > 0) {
        console.warn(`\nTypeChain dedupe: dropped ${dups.length} duplicate contract names:\n`);
    }
    console.log(`Staged ${seen.size} unique artifacts into ${STAGE_DIR}/`);
}
main();
