"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const util_1 = require("util");
const Util_1 = require("../main/Util");
const stat = util_1.promisify(fs.stat);
/**
 * Get an (incomplete) array of important file paths that should be in the
 * Flashpoint installation. The paths either absolute or relative to the
 * current working directory.
 * @param flashpointPath The path in which Flashpoint is installed. This should
 *   be grabbed from config.flashpointPath.
 */
function getImportantFiles(flashpointPath) {
    return [
        path.join(flashpointPath, './Data/Platforms'),
    ];
}
/**
 * Check if _all_ the files given exist.
 * @param files A list of paths that should exist
 * @returns true if all of the files exist, false if any of the files are missing.
 */
async function doFilesExistCheck(files) {
    const statPromiseArray = files.map(file => stat(file));
    try {
        await Promise.all(statPromiseArray);
    }
    catch (e) {
        return false;
    }
    return true;
}
/**
 * Check if the Flashpoint installation that the given path is valid.
 * @param flashpointPath The path where flashpoint is stored, can be relative.
 * @returns true if the Flashpoint installation is valid, false it it's not.
 */
async function isFlashpointValidCheck(flashpointPath) {
    const importantFiles = getImportantFiles(flashpointPath);
    return await doFilesExistCheck(importantFiles);
}
exports.isFlashpointValidCheck = isFlashpointValidCheck;
/**
 * Check if php is installed on the current system.
 * This is always true on Windows since php is bundled with Flashpoint.
 * @returns true if php is installed, false if it's not. Always returns true on Windows.
 */
async function isPhpInstalledCheck() {
    if (process.platform === 'win32') {
        // Flashpoint bundles php on windows.
        return true;
    }
    return await Util_1.isInstalled('php');
}
exports.isPhpInstalledCheck = isPhpInstalledCheck;
/**
 * Make an array of failed checks for sanityCheck to return.
 * @param isFlashpointValid Is the flashpoint installation valid?
 * @param isPhpInstalled Is php installed on this system (always true on Windows)
 */
function makeFailedChecksArray(isFlashpointValid, isPhpInstalled) {
    let failedChecks = [];
    if (!isFlashpointValid)
        failedChecks.push(FailedCheck.InvalidFlashpointInstall);
    if (!isPhpInstalled)
        failedChecks.push(FailedCheck.PhpMissing);
    return failedChecks;
}
/** The different errors that checkSanity can return */
var FailedCheck;
(function (FailedCheck) {
    /**
     * Signifies that either the flashpoint path points to something that isn't
     * flashpoint, or the flashpoint installation is incomplete/corrupted.
     */
    FailedCheck[FailedCheck["InvalidFlashpointInstall"] = 0] = "InvalidFlashpointInstall";
    /**
     * Signifies that php isn't installed. This error can only ever appear on
     * linux since php is bundled with Flashpoint on Windows.
     * The user should be asked to install the `php` package.
     */
    FailedCheck[FailedCheck["PhpMissing"] = 1] = "PhpMissing";
})(FailedCheck = exports.FailedCheck || (exports.FailedCheck = {}));
/**
 * Check if the application is ready to launch or not.
 * The setup wizard will be launched if some of the tests fail (TODO)
 * @param config The application config
 * @returns Array containing the failing tests (its empty if all tests passed)
 */
async function checkSanity(config) {
    const promiseArray = [
        isFlashpointValidCheck(config.flashpointPath),
        isPhpInstalledCheck(),
    ];
    const [doFilesExist, isPhpInstalled] = await Promise.all(promiseArray);
    return makeFailedChecksArray(doFilesExist, isPhpInstalled);
}
exports.default = checkSanity;
//# sourceMappingURL=checkSanity.js.map