package p.studio.projects;

import org.apache.commons.lang3.StringUtils;
import p.packer.messages.PackerProjectContext;
import p.studio.lsp.messages.LspProjectContext;

import java.nio.file.Path;

public record ProjectReference(
        String name,
        String version,
        String languageId,
        int stdlibVersion,
        Path rootPath) {
    @Override
    public String toString() {
        return name;
    }

    private Path absoluteRootPath() {
        return rootPath().toAbsolutePath().normalize();
    }

    private String displayRelativePath(final String resolve) {
        final Path normalizedPath = rootPath.toAbsolutePath().normalize();
        try {
            final var path = StringUtils.isNotBlank(resolve)
                    ? absoluteRootPath().resolve(resolve)
                    : absoluteRootPath();
            return path.relativize(normalizedPath).toString().replace('\\', '/');
        } catch (IllegalArgumentException ignored) {
            return normalizedPath.toString().replace('\\', '/');
        }
    }

    public PackerProjectContext toPackerProjectContext() {
        return new PackerProjectContext(name, absoluteRootPath());
    }

    public LspProjectContext toLspProjectContext() {
        return new LspProjectContext(
                rootPath().toAbsolutePath().normalize().toString(),
                languageId(),
                rootPath());
    }
}
