"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkedPorjectsConfig = exports.LinkedProject = void 0;
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
const index_1 = require("../index");
class LinkedProject {
    //#region static
    //#region static / from
    static from(options) {
        const linkedProj = lib_2._.merge(new LinkedProject(), lib_2._.cloneDeep(options));
        if (!linkedProj.relativeClonePath) {
            linkedProj.relativeClonePath = lib_2.path.basename(linkedProj.remoteUrl().replace('.git', ''));
        }
        if (!linkedProj.defaultBranch) {
            linkedProj.defaultBranch = 'master';
        }
        return linkedProj;
    }
    //#endregion
    //#region static / from name
    static fromName(projectName, currentRemoteUrl, currentBranch) {
        //#region @backendFunc
        const isProperCurrentRemoteUrl = currentRemoteUrl && index_1.HelpersTaon.git.isValidRepoUrl(currentRemoteUrl);
        const repoUrl = isProperCurrentRemoteUrl
            ? currentRemoteUrl.replace(lib_2.path.basename(currentRemoteUrl), `${projectName}.git`)
            : void 0;
        return LinkedProject.from({
            repoUrl,
            defaultBranch: currentBranch,
            relativeClonePath: projectName,
        });
        //#endregion
    }
    //#endregion
    //#region static / detect
    static detect(insideLocation, options) {
        //#region @backendFunc
        options = options || {};
        const { recursive, checkAlsoNonRepos } = options;
        insideLocation = (0, lib_2.crossPlatformPath)(insideLocation).replace(/\/$/, '');
        const detectedLinkedProjects = lib_1.Helpers.foldersFrom(insideLocation, {
            recursive,
            omitRootFolders: [
                'src',
                'environments',
                'docs',
                'node_modules',
                'dist',
                'bin',
                'old',
                'local_release',
                'projects',
                'browser',
                'websql',
            ],
            omitRootFoldersThatStartWith: ['tmp-', '.', 'dist-'],
        })
            .filter(folderAbsPath => {
            if (lib_1.notAllowedNames.includes(lib_2.path.basename(folderAbsPath))) {
                lib_1.Helpers.logWarn(`[${lib_1.config.frameworkName}-helpers][linked-projects] ` +
                    `Skipping folder ${folderAbsPath} because it has not allowed name`);
                return false;
            }
            // console.log('folderAbsPath', folderAbsPath);
            // Helpers.checkIfNameAllowedForTaonProj(path.basename(folderAbsPath)) &&
            if (checkAlsoNonRepos &&
                lib_1.Helpers.exists([folderAbsPath, lib_1.config.file.taon_jsonc]) &&
                !!lib_1.Helpers.readJsonC([folderAbsPath, lib_1.config.file.taon_jsonc])?.type &&
                lib_1.Helpers.exists([folderAbsPath, lib_1.config.file.package_json])) {
                return true;
            }
            return index_1.HelpersTaon.git.isGitRoot(folderAbsPath);
        })
            .map(folderAbsPath => {
            const relativePath = folderAbsPath.replace(insideLocation + '/', '');
            const projectName = lib_2.path.basename(relativePath);
            const currentParentUrl = index_1.HelpersTaon.git.getOriginURL(folderAbsPath);
            return LinkedProject.from({
                repoUrl: currentParentUrl ? currentParentUrl : void 0,
                relativeClonePath: projectName,
            });
        });
        // .sort((a, b) => {
        //   return (a.relativeClonePath || '').localeCompare((b.relativeClonePath || ''));
        // });
        return detectedLinkedProjects;
        //#endregion
    }
    //#endregion
    //#endregion
    /**
     * sometime there is project that has one project
     * embede inside another project
     */
    internalRealtiveProjectPath;
    /**
     * don't access directly, use remoteUrl() instead
     */
    repoUrlSsh;
    /**
     * don't access directly, use remoteUrl() instead
     */
    repoUrlHttp;
    /**
     * don't access directly, use remoteUrl() instead
     */
    repoUrl;
    /**
     * url for git repo
     */
    remoteUrl() {
        if (this.repoUrl) {
            return this.repoUrl;
        }
        if (this.repoUrlHttp) {
            return this.repoUrlHttp;
        }
        return this.repoUrlSsh;
    }
    remoteUrlTransformed(setOrigin) {
        //#region @backendFunc
        let url = this.remoteUrl();
        if (setOrigin === 'ssh') {
            if (this.repoUrlSsh) {
                return this.repoUrlSsh;
            }
            url = index_1.HelpersTaon.git.originHttpToSsh(url);
        }
        else if (setOrigin === 'http') {
            if (this.repoUrlHttp) {
                return this.repoUrlHttp;
            }
            url = index_1.HelpersTaon.git.originSshToHttp(url);
        }
        return url;
        //#endregion
    }
    defaultBranch;
    purpose;
    relativeClonePath;
}
exports.LinkedProject = LinkedProject;
class LinkedPorjectsConfig {
    //#region static
    //#region static / from
    static from(options) {
        options = options || {};
        options.projects = (options.projects || []).map(linkedProjOrname => {
            if (lib_2._.isString(linkedProjOrname)) {
                return LinkedProject.fromName(linkedProjOrname);
            }
            return LinkedProject.from(linkedProjOrname);
        });
        // .sort((a, b) => {
        //   return (a.relativeClonePath || '').localeCompare((b.relativeClonePath || ''));
        // });
        return lib_2._.merge(new LinkedPorjectsConfig(), lib_2._.cloneDeep(options));
    }
    //#endregion
    //#endregion
    prefix;
    skipRecrusivePush;
    resetOnlyChildren;
    projects;
}
exports.LinkedPorjectsConfig = LinkedPorjectsConfig;
//# sourceMappingURL=linked-project.js.map