"use strict";
const path = require("path");
const shelljs = require("shelljs");
const os = require("os");
class StaticConfigBase {
    constructor($injector) {
        this.$injector = $injector;
        this.PROJECT_FILE_NAME = null;
        this.CLIENT_NAME = null;
        this.ANALYTICS_API_KEY = null;
        this.ANALYTICS_INSTALLATION_ID_SETTING_NAME = null;
        this.TRACK_FEATURE_USAGE_SETTING_NAME = null;
        this.ERROR_REPORT_SETTING_NAME = null;
        this.APP_RESOURCES_DIR_NAME = "App_Resources";
        this.COMMAND_HELP_FILE_NAME = 'command-help.json';
        this.RESOURCE_DIR_PATH = __dirname;
        this.version = null;
        this._adbFilePath = null;
    }
    get helpTextPath() {
        return null;
    }
    getAdbFilePath() {
        return (() => {
            if (!this._adbFilePath) {
                this._adbFilePath = this.getAdbFilePathCore().wait();
            }
            return this._adbFilePath;
        }).future()();
    }
    get MAN_PAGES_DIR() {
        return path.join(__dirname, "../../", "docs", "man_pages");
    }
    get HTML_PAGES_DIR() {
        return path.join(__dirname, "../../", "docs", "html");
    }
    get HTML_COMMON_HELPERS_DIR() {
        return path.join(__dirname, "docs", "helpers");
    }
    getAdbFilePathCore() {
        return (() => {
            let $childProcess = this.$injector.resolve("$childProcess");
            try {
                let proc = $childProcess.spawnFromEvent("adb", ["version"], "exit", undefined, { throwError: false }).wait();
                if (proc.stderr) {
                    return this.spawnPrivateAdb().wait();
                }
            }
            catch (e) {
                if (e.code === "ENOENT") {
                    return this.spawnPrivateAdb().wait();
                }
            }
            return "adb";
        }).future()();
    }
    spawnPrivateAdb() {
        return (() => {
            let $fs = this.$injector.resolve("$fs"), $childProcess = this.$injector.resolve("$childProcess"), $hostInfo = this.$injector.resolve("$hostInfo");
            let defaultAdbDirPath = path.join(__dirname, `resources/platform-tools/android/${process.platform}`);
            let commonLibVersion = require(path.join(__dirname, "package.json")).version;
            let tmpDir = path.join(os.tmpdir(), `telerik-common-lib-${commonLibVersion}`);
            $fs.createDirectory(tmpDir).wait();
            let targetAdb = path.join(tmpDir, "adb");
            if (!$fs.exists(tmpDir).wait() || !$fs.readDirectory(tmpDir).wait().length) {
                shelljs.cp(path.join(defaultAdbDirPath, "*"), tmpDir);
                if (!$hostInfo.isWindows) {
                    shelljs.chmod("+x", targetAdb);
                }
            }
            $childProcess.spawnFromEvent(targetAdb, ["start-server"], "exit").wait();
            return targetAdb;
        }).future()();
    }
}
exports.StaticConfigBase = StaticConfigBase;
