// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0
import "./_dnt.polyfills.js";
import { wit_utils } from "./wit.js";
import { freezeTgOutput } from "./utils/func_utils.js";
export class Metagen {
    constructor(workspacePath, genConfig) {
        Object.defineProperty(this, "workspacePath", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: workspacePath
        });
        Object.defineProperty(this, "genConfig", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: genConfig
        });
    }
    getFdkConfig(tgOutput, targetName) {
        const serializeParams = {
            typegraphPath: `${this.workspacePath}/tg.ts`,
            prefix: undefined,
            artifactResolution: false,
            codegen: true,
            prismaMigration: {
                migrationsDir: "prisma-migrations",
                migrationActions: [],
                defaultMigrationAction: {
                    apply: false,
                    create: false,
                    reset: false,
                },
            },
            pretty: false,
        };
        const frozenOut = freezeTgOutput(serializeParams, tgOutput);
        return {
            configJson: JSON.stringify(this.genConfig),
            tgJson: frozenOut.serialize(serializeParams).tgJson,
            targetName,
            workspacePath: this.workspacePath,
        };
    }
    /** dry-run metagen */
    dryRun(tgOutput, targetName, overwrite) {
        const fdkConfig = this.getFdkConfig(tgOutput, targetName);
        return wit_utils.metagenExec(fdkConfig).map((value) => ({
            ...value,
            overwrite: overwrite ?? value.overwrite,
        }));
    }
    /** run metagen */
    run(tgOutput, targetName, overwrite) {
        const items = this.dryRun(tgOutput, targetName, overwrite);
        wit_utils.metagenWriteFiles(items, this.workspacePath);
    }
}
