package p.studio.compiler.models;

import p.studio.compiler.source.Span;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.identifiers.ModuleId;

import java.util.Objects;

public record IRGlobal(
        FileId fileId,
        ModuleId moduleId,
        String name,
        String declaredTypeSurface,
        int slot,
        IRGlobalVisibility visibility,
        boolean isHidden,
        IRHiddenGlobalKind hiddenKind,
        IRGlobalOrigin origin,
        Span span) {
    public IRGlobal {
        fileId = Objects.requireNonNull(fileId, "fileId");
        moduleId = moduleId == null ? ModuleId.none() : moduleId;
        name = Objects.requireNonNull(name, "name");
        declaredTypeSurface = declaredTypeSurface == null ? "" : declaredTypeSurface;
        slot = Math.max(0, slot);
        visibility = visibility == null ? IRGlobalVisibility.MODULE : visibility;
        hiddenKind = hiddenKind == null ? IRHiddenGlobalKind.NONE : hiddenKind;
        isHidden = isHidden || hiddenKind != IRHiddenGlobalKind.NONE;
        span = span == null ? Span.none() : span;
        origin = origin == null
                ? new IRGlobalOrigin(fileId, moduleId, name, span, name)
                : origin;
    }

    public IRGlobal(
            final FileId fileId,
            final ModuleId moduleId,
            final String name,
            final String declaredTypeSurface,
            final Span span) {
        this(
                fileId,
                moduleId,
                name,
                declaredTypeSurface,
                0,
                IRGlobalVisibility.MODULE,
                false,
                IRHiddenGlobalKind.NONE,
                new IRGlobalOrigin(fileId, moduleId, name, span, name),
                span);
    }
}
