"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseJsonFileReader = void 0;
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
/**
 * Base class for reading json files
 * There are 2 modes of reading json files:
 * - cwd + fileName (read and write to disk in cwd)
 * - jsonContent (read and write to memory and use
 *   reloadInMemoryCallback for external reload)
 */
class BaseJsonFileReader {
    //#region fields
    /**
     * null when package.json not exist
     */
    data;
    /**
     * use json with comments reader to read package.json
     */
    readJsonAsJsonWithComments = false;
    //#endregion
    //#region constructor
    cwd;
    fileName;
    /**
     * @deprecated
     * use only when cwd and fileName are not provided
     */
    jsonContent;
    defaultValue;
    reloadInMemoryCallback;
    constructor(options) {
        let { cwd, fileName, jsonContent, reloadInMemoryCallback, readJsonAsJsonWithComments, defaultValue, } = options;
        if (cwd) {
            cwd = (0, lib_2.crossPlatformPath)(cwd);
            if (cwd.endsWith(`/${lib_1.config.file.package_json}`)) {
                cwd = cwd.replace(`/${lib_1.config.file.package_json}`, '');
            }
            this.cwd = cwd;
        }
        this.defaultValue = defaultValue;
        this.fileName = fileName;
        if (cwd && jsonContent) {
            throw new Error(`You can't provide cwd and jsonContent at the same time. Choose one.`);
        }
        if (!cwd && !jsonContent) {
            throw new Error(`You need to provide cwd or jsonContent`);
        }
        this.readJsonAsJsonWithComments = !!readJsonAsJsonWithComments;
        this.reloadInMemoryCallback = reloadInMemoryCallback;
        this.jsonContent = lib_2._.cloneDeep(jsonContent);
        this.reloadPackageJsonInMemory();
    }
    //#endregion
    //#region path
    /**
     * cwd + fileName
     */
    get path() {
        return (0, lib_2.crossPlatformPath)([this.cwd, this.fileName]);
    }
    //#endregion
    //#region reload package json in memory
    /**
     * if something else change package.json in this project
     * and you know that you need to reload it..
     */
    reloadPackageJsonInMemory() {
        if (this.jsonContent) {
            this.data = lib_2._.cloneDeep(this.jsonContent);
        }
        else {
            try {
                if (this.readJsonAsJsonWithComments) {
                    this.data = lib_1.Helpers.readJsonC(this.path);
                }
                else {
                    this.data = lib_1.Helpers.readJson(this.path);
                }
                if (!this.data && this.defaultValue) {
                    this.data = lib_2._.cloneDeep(this.defaultValue);
                }
            }
            catch (error) {
                if (this.defaultValue) {
                    this.data = lib_2._.cloneDeep(this.defaultValue);
                }
                else {
                    this.data = null;
                }
            }
        }
        this.reloadInMemoryCallback &&
            this.reloadInMemoryCallback(lib_2._.cloneDeep(this.data));
    }
    //#endregion
    //#region write package json to disk
    saveToDisk(purpose) {
        //#region @backendFunc
        if (this.jsonContent) {
            // @ts-ignore
            this.jsonContent = lib_2._.cloneDeep(this.data);
        }
        else {
            if (!this.data) {
                lib_1.Helpers.warn(`[taon][saveToDisk] Empty object to save in ${this.fileName} in ${this.cwd}`);
                return;
            }
            lib_1.Helpers.log(`Save package.json to disk ${purpose ? `(${purpose})` : ''} in ${this.cwd}`);
            lib_1.Helpers.writeJson(this.path, this.data);
        }
        this.reloadPackageJsonInMemory();
        //#endregion
    }
    //#endregion
    //#region link to
    linkTo(destination) {
        //#region @backendFunc
        const source = lib_2.path.join(this.cwd, this.fileName);
        const dest = lib_2.path.join(destination, this.fileName);
        lib_1.Helpers.removeFileIfExists(dest);
        lib_1.Helpers.createSymLink(source, dest);
        //#endregion
    }
    //#endregion
    //#region get all data
    getAllData() {
        return lib_2._.cloneDeep(this.data);
    }
    //#endregion
    //#region set all data
    setAllData(data) {
        this.data = lib_2._.cloneDeep(data);
        this.saveToDisk();
    }
}
exports.BaseJsonFileReader = BaseJsonFileReader;
//# sourceMappingURL=base-json-file-reader.js.map