import { existsSync, readFileSync, statSync } from 'fs';
import { rm } from 'fs/promises';
import { isAbsolute, join, relative } from 'path';
import { lt as semverLessThan } from 'semver';
import { defaults } from 'lodash-es';
import { create } from 'mem-fs-editor';
import { GENERATOR_JHIPSTER } from '../generator-constants.js';
export default class SharedData {
    _storage;
    _editor;
    _log;
    _logCwd;
    constructor(storage, { memFs, destinationPath, log, logCwd }, initialControl = {}) {
        if (!storage) {
            throw new Error('Storage is required for SharedData');
        }
        this._editor = create(memFs);
        this._log = log;
        this._logCwd = logCwd;
        let jhipsterOldVersion;
        if (existsSync(join(destinationPath, '.yo-rc.json'))) {
            jhipsterOldVersion = JSON.parse(readFileSync(join(destinationPath, '.yo-rc.json'), 'utf-8').toString())[GENERATOR_JHIPSTER]
                ?.jhipsterVersion;
        }
        this._storage = storage;
        defaults(this._storage, {
            sharedDeployment: {},
            sharedWorkspaces: {},
            sharedEntities: {},
            sharedApplication: {},
            sharedSource: {},
            control: initialControl,
            props: {},
        });
        defaults(this._storage.sharedApplication, {
            nodeDependencies: {},
            customizeTemplatePaths: [],
        });
        let customizeRemoveFiles = [];
        const removeFiles = async (assertions, ...files) => {
            if (typeof assertions === 'string') {
                files = [assertions, ...files];
                assertions = {};
            }
            for (const customize of customizeRemoveFiles) {
                files = files.map(customize).filter(file => file);
            }
            const { removedInVersion } = assertions;
            if (removedInVersion && jhipsterOldVersion && !semverLessThan(jhipsterOldVersion, removedInVersion)) {
                return;
            }
            const absolutePaths = files.map(file => (isAbsolute(file) ? file : join(destinationPath, file)));
            this._editor.delete(absolutePaths);
            await Promise.all(absolutePaths.map(async (file) => {
                const relativePath = relative(logCwd, file);
                try {
                    if (statSync(file).isFile()) {
                        this._log.info(`Removing legacy file ${relativePath}`);
                        await rm(file, { force: true });
                    }
                }
                catch {
                    this._log.info(`Could not remove legacy file ${relativePath}`);
                }
            }));
        };
        defaults(this._storage.control, {
            jhipsterOldVersion,
            removeFiles,
            customizeRemoveFiles: [],
            cleanupFiles: async (cleanup) => {
                await Promise.all(Object.entries(cleanup).map(async ([version, files]) => {
                    const stringFiles = [];
                    for (const file of files) {
                        if (Array.isArray(file)) {
                            const [condition, ...fileParts] = file;
                            if (condition) {
                                stringFiles.push(join(...fileParts));
                            }
                        }
                        else {
                            stringFiles.push(file);
                        }
                    }
                    await removeFiles({ removedInVersion: version }, ...stringFiles);
                }));
            },
        });
        customizeRemoveFiles = this._storage.control.customizeRemoveFiles;
    }
    getSource() {
        return this._storage.sharedSource;
    }
    getControl() {
        return this._storage.control;
    }
    getApplication() {
        if (!this._storage.sharedApplication)
            throw new Error('Shared application not loaded');
        return this._storage.sharedApplication;
    }
    setEntity(entityName, entity) {
        this._storage.sharedEntities[entityName] = entity;
    }
    hasEntity(entityName) {
        return Boolean(this._storage.sharedEntities[entityName]);
    }
    getEntity(entityName) {
        const entity = this._storage.sharedEntities[entityName];
        if (!entity) {
            throw new Error(`Entity definition not loaded for ${entityName}`);
        }
        return entity;
    }
    getEntities(entityNames = Object.keys(this._storage.sharedEntities)) {
        return entityNames.map(entityName => ({ entityName, entity: this.getEntity(entityName) }));
    }
    getEntitiesMap() {
        return this._storage.sharedEntities;
    }
}
