const windowsInstaller = require('./src/installer/WindowsInstaller');
const linuxInstaller = require('./src/installer/LinuxInstaller');

const PSLogger = require('@sisense-ps/ps-logger/index');
const { messageMap } = require('./src/constants/Messages');
const HandledError = require('./src/errors/HandledError');

const Utils = require('./src/Utils');
const passedArguments = Utils.parseArguments(process.argv);


if (Utils.getOsType() === 'Linux') {
    linuxInstaller.install(passedArguments).then((result) => {
        PSLogger.log(messageMap[`${result.data.name.toUpperCase()}_COMPLETE`]);
    }).catch((error) => {
        console.dir(error);
    });
} else {
    windowsInstaller.actionHandler(passedArguments).then((result) => {
        if (!result.isSuccess) {
            console.error(result.error);
            return;
        }
        if (!result.isDuplicated) {
            PSLogger.log(messageMap[`${result.data.name.toUpperCase()}_COMPLETE`]);
        }
    }).catch((error) => {
        if (error instanceof HandledError) {
            PSLogger.log(messageMap.HANDLED_ERROR, { reason: error.message });
        } else {
            console.dir(error);
        }
    });
}
