export class MaterialVariantWorkflow {
    states = new Map();
    register(assetId, variants, selected = null) {
        const normalizedAssetId = normalizeAssetId(assetId);
        const normalizedVariants = normalizeVariants(variants);
        const normalizedSelected = selected === null ? null : normalizeVariantName(selected);
        if (normalizedSelected !== null && !normalizedVariants.includes(normalizedSelected)) {
            throw new Error(`Material variant ${normalizedSelected} is not available for asset ${normalizedAssetId}.`);
        }
        const state = {
            assetId: normalizedAssetId,
            variants: normalizedVariants,
            selected: normalizedSelected
        };
        this.states.set(normalizedAssetId, state);
        return state;
    }
    unregister(assetId) {
        this.states.delete(normalizeAssetId(assetId));
    }
    select(assetId, variant) {
        const normalizedAssetId = normalizeAssetId(assetId);
        const state = this.states.get(normalizedAssetId);
        if (!state) {
            throw new Error(`Material variant asset ${normalizedAssetId} is not registered.`);
        }
        const selected = variant === null ? null : normalizeVariantName(variant);
        if (selected !== null && !state.variants.includes(selected)) {
            throw new Error(`Material variant ${selected} is not available for asset ${normalizedAssetId}.`);
        }
        const next = { ...state, selected };
        this.states.set(normalizedAssetId, next);
        return next;
    }
    state(assetId) {
        const normalizedAssetId = normalizeAssetId(assetId);
        const state = this.states.get(normalizedAssetId);
        if (!state) {
            throw new Error(`Material variant asset ${normalizedAssetId} is not registered.`);
        }
        return state;
    }
    snapshot() {
        return [...this.states.values()].map((state) => ({
            assetId: state.assetId,
            variants: [...state.variants],
            selected: state.selected
        }));
    }
    renderOptions(assetId) {
        const selected = this.state(assetId).selected;
        return selected === null ? {} : { materialVariant: selected };
    }
    clear() {
        this.states.clear();
    }
}
function normalizeAssetId(assetId) {
    const normalized = assetId.trim();
    if (normalized.length === 0) {
        throw new Error("Material variant asset id cannot be empty.");
    }
    return normalized;
}
function normalizeVariantName(variant) {
    const normalized = variant.trim();
    if (normalized.length === 0) {
        throw new Error("Material variant name cannot be empty.");
    }
    return normalized;
}
function normalizeVariants(variants) {
    if (variants.length === 0) {
        throw new Error("Material variant list cannot be empty.");
    }
    const normalized = variants.map(normalizeVariantName);
    if (new Set(normalized).size !== normalized.length) {
        throw new Error("Material variant list cannot contain duplicate names.");
    }
    return normalized;
}
//# sourceMappingURL=MaterialVariantWorkflow.js.map