import { hash } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
import { createPoolConfigFromJson } from "../keystone-config-builder.mjs";
import profileHigh from "./profiles/profile-high.json" assert { type: "json" };
import profileMedium from "./profiles/profile-medium.json" assert { type: "json" };
import profileLow from "./profiles/profile-low.json" assert { type: "json" };
import profileSession from "./profiles/profile-session.json" assert { type: "json" };
import profileTest from "./profiles/profile-test.json" assert { type: "json" };
export class KeystoneProfileBuilder {
    _template;
    _username;
    _email;
    _description;
    _details;
    _customizations = {};
    constructor(template) {
        // Deep copy the template to avoid modifying shared imports
        this._template = JSON.parse(JSON.stringify(template));
    }
    static buildKeystone(profileName) {
        let selected;
        switch (profileName) {
            case 'high':
                selected = profileHigh;
                break;
            case 'medium':
                selected = profileMedium;
                break;
            case 'low':
                selected = profileLow;
                break;
            case 'session':
                selected = profileSession;
                break;
            case 'test':
                selected = profileTest;
                break;
            default:
                throw new Error(`Unknown profile name: ${profileName} (E: fc10a8ad482ef2bda871ac567d268d26)`);
        }
        return new KeystoneProfileBuilder(selected);
    }
    withUsername(username) {
        this._username = username;
        return this;
    }
    withEmail(email) {
        this._email = email;
        return this;
    }
    withDescription(description) {
        this._description = description;
        return this;
    }
    withDetails(details) {
        this._details = details;
        return this;
    }
    customizePool(poolId, overrides) {
        const { behavior, ...templateOverrides } = overrides;
        this._customizations[poolId] = {
            template: templateOverrides,
            behavior
        };
        return this;
    }
    /**
     * Unifies username, email, description, and details into a frameDetails object for genesis.
     */
    getFrameDetails() {
        const details = { ...this._details };
        if (this._username) {
            details.username = this._username;
        }
        if (this._email) {
            details.email = this._email;
        }
        if (this._description) {
            details.description = this._description;
        }
        return details;
    }
    async compileConfigs() {
        const poolConfigs = [];
        // Derive a unique user-specific base salt if username/email is provided
        let userSalt = "";
        if (this._username || this._email) {
            const seed = `${this._username || ""}:${this._email || ""}`;
            userSalt = await hash({ s: seed });
        }
        else {
            // Fallback salt to guarantee unique challenge IDs even without user details
            userSalt = await hash({ s: "default-keystone-salt" });
        }
        for (const [poolKey, poolTemplate] of Object.entries(this._template.pools)) {
            // Apply customization overrides
            const custom = this._customizations[poolTemplate.id];
            if (custom) {
                if (custom.template) {
                    Object.assign(poolTemplate, custom.template);
                }
                if (custom.behavior && poolTemplate.behavior) {
                    Object.assign(poolTemplate.behavior, custom.behavior);
                }
            }
            // Derive a unique salt for this specific pool by hashing userSalt + poolId
            const saltSeed = `${userSalt}-${poolTemplate.id}`;
            const derivedSalt = await hash({ s: saltSeed });
            const poolConfig = createPoolConfigFromJson({
                template: poolTemplate,
                salt: derivedSalt
            });
            poolConfigs.push(poolConfig);
        }
        return poolConfigs;
    }
}
//# sourceMappingURL=keystone-profile-builder.mjs.map