"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.systemIsSimulatorReady = systemIsSimulatorReady;
exports.systemIsEmulatorReady = systemIsEmulatorReady;
exports.checkJavaSdk = checkJavaSdk;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const OperatingSystemDescriptor_1 = require("@mablhq/test-model/types/OperatingSystemDescriptor");
const androidUtil_1 = require("./androidUtil");
const iOSUtil_1 = require("./iOSUtil");
async function systemIsSimulatorReady(device) {
    const osCheck = {
        requirementsMet: false,
    };
    const operatingSystem = (0, OperatingSystemDescriptor_1.getOperatingSystem)();
    if (operatingSystem !== OperatingSystemDescriptor_1.OperatingSystem.MACOS) {
        osCheck.requirementsMet = false;
        osCheck.informationalText = `Current system is running ${operatingSystem}. Mac OS is required for iOS tests`;
    }
    else {
        osCheck.requirementsMet = true;
        osCheck.informationalText = `Operating system detected: ${operatingSystem}`;
    }
    const xcodeCheck = {
        requirementsMet: false,
    };
    try {
        const xcodeVersion = await (0, iOSUtil_1.getXcodeVersion)();
        if (xcodeVersion) {
            xcodeCheck.requirementsMet = true;
            xcodeCheck.informationalText = `Xcode version ${xcodeVersion} found`;
        }
        else {
            xcodeCheck.requirementsMet = false;
            xcodeCheck.informationalText = `Xcode not detected on system`;
        }
    }
    catch (error) {
        console.error(`Error running Xcode check`, error);
        xcodeCheck.requirementsMet = false;
        xcodeCheck.informationalText = `Xcode not detected - ${error?.message}`;
    }
    const xcRunCheck = {
        requirementsMet: false,
    };
    try {
        const xcRunInstalled = (0, iOSUtil_1.isXcRunInstalled)();
        if (xcRunInstalled) {
            xcRunCheck.requirementsMet = true;
            xcRunCheck.informationalText = `xcrun command line tool detected`;
            if (device) {
                const simulators = await (0, iOSUtil_1.listSimulators)();
                if (!Object.keys(simulators).some((simulator) => simulator === device)) {
                    xcRunCheck.requirementsMet = false;
                    xcRunCheck.informationalText = `Simulator ${device} was not found`;
                    console.log(`Simulators found: ${Object.keys(simulators)}`);
                }
            }
        }
        else {
            xcRunCheck.requirementsMet = false;
            xcRunCheck.informationalText = `xcrun command line tool not detected`;
        }
    }
    catch (error) {
        console.error(`Error running xcrun check`, error);
        xcRunCheck.requirementsMet = false;
        xcRunCheck.informationalText = `xcrun command line tool not detected  - ${error?.message}`;
    }
    return Promise.resolve({
        requirementsMet: osCheck.requirementsMet &&
            xcodeCheck.requirementsMet &&
            xcRunCheck.requirementsMet,
        checks: {
            operatingSystem: osCheck,
            xcode: xcodeCheck,
            xcrunCommand: xcRunCheck,
        },
    });
}
async function systemIsEmulatorReady(deviceName, includePhysicalDevices) {
    const operatingSystem = (0, OperatingSystemDescriptor_1.getOperatingSystem)();
    const osCheck = {
        requirementsMet: false,
    };
    if (![
        OperatingSystemDescriptor_1.OperatingSystem.LINUX,
        OperatingSystemDescriptor_1.OperatingSystem.MACOS,
        OperatingSystemDescriptor_1.OperatingSystem.WINDOWS,
    ].includes(operatingSystem)) {
        osCheck.requirementsMet = false;
        osCheck.informationalText = `Current system is running ${operatingSystem}. Mac OS is required for iOS tests`;
    }
    else {
        osCheck.requirementsMet = true;
        osCheck.informationalText = `Operating system detected: ${operatingSystem}`;
    }
    const androidSdkCheck = {
        requirementsMet: false,
    };
    const androidHomePath = (0, androidUtil_1.getAndroidHomeDirectoryPath)();
    console.log(`Android home: ${androidHomePath}`);
    if (!fs.existsSync(androidHomePath)) {
        console.log(`Android home does not exist: ${androidHomePath}`);
        androidSdkCheck.requirementsMet = false;
        androidSdkCheck.informationalText = `Android home directory not located. Tried: ${androidHomePath}`;
    }
    else {
        console.log(`Android home exists: ${androidHomePath}`);
        androidSdkCheck.requirementsMet = true;
        androidSdkCheck.informationalText = `Android home directory found at: ${androidHomePath}`;
    }
    const emulatorsCheck = {
        requirementsMet: false,
    };
    const emulators = await (0, androidUtil_1.getListOfLocalAndroidEmulators)(includePhysicalDevices);
    const emulatorsCount = Object.values(emulators).length;
    if (emulatorsCount > 0) {
        emulatorsCheck.requirementsMet = true;
        emulatorsCheck.informationalText = `Number of emulators found: ${emulatorsCount}`;
        if (deviceName) {
            const foundDevice = Object.keys(emulators).find((emulator) => emulator === deviceName);
            if (!foundDevice) {
                console.debug(`Emulators found: `);
                Object.values(emulators).forEach((element) => {
                    console.log(`   ${element.avdName}`);
                });
                emulatorsCheck.informationalText = `Emulator ${deviceName} was not found`;
                emulatorsCheck.requirementsMet = false;
            }
        }
    }
    else {
        emulatorsCheck.informationalText = `Couldn't find any emulator`;
    }
    return {
        requirementsMet: osCheck.requirementsMet &&
            androidSdkCheck.requirementsMet &&
            emulatorsCheck.requirementsMet,
        checks: {
            operatingSystem: osCheck,
            androidSdkCheck,
            emulatorsCheck,
        },
    };
}
function checkJavaSdk() {
    const JAVA_BINARY_RELATIVE_PATH = 'bin/java';
    const javaSdkCheck = {
        requirementsMet: false,
    };
    if (!process.env.JAVA_HOME) {
        javaSdkCheck.informationalText = `Couldn't find Java SDK installed. Environment variable JAVA_HOME is not set.`;
        return javaSdkCheck;
    }
    const javaBinaryFullPath = path.resolve(process.env.JAVA_HOME, path.normalize(JAVA_BINARY_RELATIVE_PATH));
    if (!fs.existsSync(javaBinaryFullPath)) {
        javaSdkCheck.informationalText = `Couldn't find Java SDK installed. Please verify that the JAVA_HOME environment variable has the correct path.`;
        return javaSdkCheck;
    }
    javaSdkCheck.requirementsMet = true;
    const javaVersion = (0, androidUtil_1.getJavaVersion)(javaBinaryFullPath);
    javaSdkCheck.informationalText = `Java SDK version: ${javaVersion ?? 'N/A'}`;
    return javaSdkCheck;
}
