package p.studio.compiler.messages;

import p.studio.utilities.structures.ReadOnlyList;

import java.util.ArrayList;
import java.util.LinkedHashMap;

public record FESurfaceContext(
        ReadOnlyList<Addressable> assets) {

    public FESurfaceContext {
        assets = normalizeAssets(assets);
    }

    public static FESurfaceContext empty() {
        return new FESurfaceContext(ReadOnlyList.empty());
    }

    private static ReadOnlyList<Addressable> normalizeAssets(final ReadOnlyList<Addressable> assets) {
        if (assets == null || assets.isEmpty()) {
            return ReadOnlyList.empty();
        }
        final var dedup = new LinkedHashMap<String, Addressable>();
        for (final var asset : assets) {
            if (asset == null || asset.address().isBlank()) {
                continue;
            }
            dedup.put(asset.address(), asset);
        }
        return ReadOnlyList.wrap(new ArrayList<>(dedup.values()));
    }
}
