const fs = require('fs'),
    chalk = require('chalk'),
    ini = require('ini');

const removeFromArray = (arr, item) => {
    "use strict";
    for (var i = arr.length; i--;) {
        if (arr[i] === item) {
            arr.splice(i, 1);
        }
    }
}

const iniContol = {
    write(iniFilePath, iniObject) {
        let status = false;
        try {
            fs.writeFileSync(iniFilePath, ini.stringify(iniObject, { section: '' }))
            status = true;
        } catch (e) {
            console.log(chalk.bgBlue.red('Error while writing ini file'));
        }

        return status;
    },

    read(iniFilePath) {
        let iniObject = {};

        try {
            iniObject = ini.parse(fs.readFileSync(iniFilePath, 'utf-8'));
        } catch (error) {
            console.log(chalk.bgBlue.red(`Error while reading ini file: ${iniFilePath}`));
        }

        return iniObject;
    }
}

module.exports = {
    removeFromArray: removeFromArray,
    iniContol: iniContol
}