import { get, set } from 'lodash-es';
import XmlStorage from '../internal/xml-store.js';
import { formatPomFirstLevel, sortPomProject } from '../internal/pom-sort.js';
const artifactEquals = (a, b) => a.groupId === b.groupId && a.artifactId === b.artifactId;
const dependencyEquals = (a, b) => artifactEquals(a, b) && a.scope === b.scope && a.type === b.type;
const idEquals = (a, b) => a.id === b.id;
const ensureChildIsArray = (node, childPath) => {
    let dependencyArray = get(node, childPath);
    if (!dependencyArray) {
        dependencyArray = [];
        set(node, childPath, dependencyArray);
    }
    else if (!Array.isArray(dependencyArray)) {
        dependencyArray = [dependencyArray];
        set(node, childPath, dependencyArray);
    }
    return dependencyArray;
};
function appendOrReplace(array, item, equals) {
    const dependencyIndex = array.findIndex(existing => equals(existing, item));
    if (dependencyIndex === -1) {
        array.push(item);
    }
    else {
        array[dependencyIndex] = item;
    }
}
function appendOrGet(array, item, equals) {
    const child = array.find(existing => equals(existing, item));
    if (child) {
        return child;
    }
    array.push(item);
    return item;
}
const ensureProfile = (project, profileId) => {
    const profileArray = ensureChildIsArray(project, 'profiles.profile');
    return appendOrGet(profileArray, { id: profileId }, idEquals);
};
const ensureChildPath = (node, childPath) => {
    let child = get(node, childPath);
    if (child)
        return child;
    child = {};
    set(node, childPath, child);
    return child;
};
const ensureChild = (current, ...childPath) => {
    for (const node of childPath) {
        if (typeof node === 'string') {
            current = ensureChildPath(current, node);
        }
        else if (typeof node === 'function') {
            current = node(current);
        }
        else {
            throw new Error(`Path section not supported ${node}`);
        }
        if (!current) {
            return undefined;
        }
    }
    return current;
};
const reorderArtifact = ({ groupId, artifactId, inProfile, version, ...rest }) => ({ inProfile, groupId, artifactId, version, ...rest });
const reorderDependency = ({ groupId, artifactId, inProfile, version, type, scope, classifier, ...rest }) => ({ inProfile, groupId, artifactId, version, type, scope, classifier, ...rest });
export default class PomStorage extends XmlStorage {
    constructor({ saveFile, loadFile, sortFile }) {
        super({ saveFile, loadFile, sortFile });
    }
    addProperty({ inProfile, property, value = null }) {
        const node = this.getNode({ nodePath: 'properties', profile: inProfile });
        node[property] = value;
        this.persist();
    }
    addDependency({ inProfile, ...dependency }) {
        this.addDependencyAt(this.getNode({ profile: inProfile }), reorderDependency(dependency));
        this.persist();
    }
    addDependencyManagement({ inProfile, ...dependency }) {
        this.addDependencyAt(this.getNode({ profile: inProfile, nodePath: 'dependencyManagement' }), reorderDependency(dependency));
        this.persist();
    }
    addDistributionManagement({ inProfile, snapshotsId, snapshotsUrl, releasesId, releasesUrl }) {
        const store = this.getNode({ profile: inProfile });
        store.distributionManagement = {
            snapshotRepository: {
                id: snapshotsId,
                url: snapshotsUrl,
            },
            repository: {
                id: releasesId,
                url: releasesUrl,
            },
        };
        this.persist();
    }
    addProfile({ content, ...profile }) {
        const profileArray = ensureChildIsArray(this.getNode(), 'profiles.profile');
        appendOrReplace(profileArray, this.mergeContent(profile, content), idEquals);
        this.persist();
    }
    addPlugin({ inProfile, ...plugin }) {
        this.addPluginAt(this.getNode({ profile: inProfile, nodePath: 'build' }), plugin);
        this.persist();
    }
    addPluginManagement({ inProfile, ...plugin }) {
        this.addPluginAt(this.getNode({ profile: inProfile, nodePath: 'build.pluginManagement' }), plugin);
        this.persist();
    }
    addRepository({ inProfile, ...repository }) {
        this.addRepositoryAt(this.getNode({ profile: inProfile }), repository);
        this.persist();
    }
    addPluginRepository({ inProfile, ...repository }) {
        this.addPluginRepositoryAt(this.getNode({ profile: inProfile }), repository);
        this.persist();
    }
    addAnnotationProcessor({ inProfile, ...artifact }) {
        const node = this.getNode({ profile: inProfile });
        const plugins = ensureChildIsArray(node, 'build.pluginManagement.plugins.plugin');
        const annotationProcessorPaths = ensureChild(plugins, pluginArray => {
            return appendOrGet(pluginArray, {
                groupId: 'org.apache.maven.plugins',
                artifactId: 'maven-compiler-plugin',
            }, artifactEquals);
        }, 'configuration.annotationProcessorPaths');
        const paths = ensureChildIsArray(annotationProcessorPaths, 'path');
        appendOrReplace(paths, reorderArtifact(artifact), artifactEquals);
        this.persist();
    }
    getNode({ profile, nodePath } = {}) {
        const node = profile ? ensureProfile(this.store.project, profile) : this.store.project;
        if (nodePath) {
            return ensureChild(node, nodePath);
        }
        return node;
    }
    addDependencyAt(node, { additionalContent, ...dependency }) {
        const dependencyArray = ensureChildIsArray(node, 'dependencies.dependency');
        appendOrReplace(dependencyArray, this.mergeContent(reorderDependency(dependency), additionalContent), dependencyEquals);
    }
    addPluginAt(node, { additionalContent, ...artifact }) {
        const artifactArray = ensureChildIsArray(node, 'plugins.plugin');
        appendOrReplace(artifactArray, this.mergeContent(reorderArtifact(artifact), additionalContent), artifactEquals);
    }
    addRepositoryAt(node, { releasesEnabled, snapshotsEnabled, ...repository }) {
        const releases = releasesEnabled === undefined ? undefined : { enabled: releasesEnabled };
        const snapshots = snapshotsEnabled === undefined ? undefined : { enabled: snapshotsEnabled };
        const repositoryArray = ensureChildIsArray(node, 'repositories.repository');
        appendOrReplace(repositoryArray, { ...repository, releases, snapshots }, idEquals);
    }
    addPluginRepositoryAt(node, { releasesEnabled, snapshotsEnabled, ...repository }) {
        const releases = releasesEnabled === undefined ? undefined : { enabled: releasesEnabled };
        const snapshots = snapshotsEnabled === undefined ? undefined : { enabled: snapshotsEnabled };
        const repositoryArray = ensureChildIsArray(node, 'pluginRepositories.pluginRepository');
        appendOrReplace(repositoryArray, { ...repository, releases, snapshots }, idEquals);
    }
    sort() {
        if (this.store.project) {
            this.store.project = sortPomProject(this.store.project);
        }
    }
}
const emptyPomFile = `<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
</project>
`;
export const createPomStorage = (generator, { sortFile } = {}) => {
    const loadFile = () => generator.readDestination('pom.xml', { defaults: emptyPomFile })?.toString() ?? '';
    const pomStorage = new PomStorage({
        loadFile,
        saveFile: content => generator.writeDestination('pom.xml', formatPomFirstLevel(content)),
        sortFile,
    });
    generator.fs.store.on('change', filename => {
        if (filename === generator.destinationPath('pom.xml')) {
            pomStorage.clearCache();
        }
    });
    return pomStorage;
};
