"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModList = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const crypto_1 = require("crypto");
class ModList {
    get modFileNames() {
        return this.mods.map((m) => {
            return m + '.zip';
        });
    }
    get factorioModListFormat() {
        return {
            mods: this.mods.map((m) => {
                return {
                    name: m.split('_')[0],
                    enabled: true
                };
            })
        };
    }
    // writes the mod-list.json file format - filepath should be absolute, and have 'mod-list.json' (or whatever filename) at the end
    writeModListFile(filepath) {
        return __awaiter(this, void 0, void 0, function* () {
            yield fs_extra_1.default.writeFile(filepath, JSON.stringify(this.factorioModListFormat, null, 4));
        });
    }
    // takes in an array of mod names, supports many naming types
    // converts name into a name+version object, if no version is specified it will be queried from the API, and the latest version will be locked in
    constructor(params) {
        this.mods = [];
        if (!params)
            return;
        this.id = (0, crypto_1.randomUUID)();
        this.name = params.name;
        this.desc = params.desc;
        if (!params.mods)
            params.mods = [];
        else if (params.mods.length > 0) {
            for (let i = 0; i < params.mods.length; i++) {
                let mod = params.mods[i];
                mod = mod.endsWith('.zip') ? mod.substring(0, mod.length - 4) : mod;
                // Find the first '.', which will help is identify the version number and string format
                let firstDotIndex = this.name.indexOf('.');
                let lastUnderscoreIndex = this.name.lastIndexOf('_');
                if (firstDotIndex === -1 || lastUnderscoreIndex === -1) {
                    // no version, just mod name itself
                    // set to latest. When mod caching comes around to check it, will grab latest version and change this
                    mod = mod + '_latest';
                }
                this.mods.push(mod);
            }
        }
    }
}
exports.ModList = ModList;
//# sourceMappingURL=ModList.js.map