
String.prototype.format = function() {
    let formatted = this;
    for (let i = 0; i < arguments.length; i++) {
        let regexp = new RegExp('\\{' + i + '\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
};

function itemOf(tag) {
    var stacks = toStacks(tag);
    if(stacks.length>0){
        stacks.sort((a,b) => compareMod(a.mod,b.mod));
        return stacks[0].id;
    }else{
        return null;
    }
}

function fluidOf(material) {

    if(material == 'netherite_scrap') return 'tconstruct:molten_debris';
    if(material == 'silicon_bronze') return 'tconstruct:molten_tinkers_bronze';

    let tconstruct = `tconstruct:molten_${material}`,
        materialis = `materialis:molten_${material}`,
        ee = `emendatusenigmatica:molten_${material}`,
        jaopca = `jaopca:molten.${material}`;

    if(Fluid.exists(tconstruct)) {
        return tconstruct;
    }
    if(Fluid.exists(materialis)) {
        return materialis;
    }
    if(Fluid.exists(ee)) {
        return ee;
    }
    if(Fluid.exists(jaopca)) {
        return jaopca;
    }

    return null;
}

function compareMod(a,b){
    if(a==b) return 0;
    for(let mod of modIds){
        if(a == mod) return -1;
        if(b == mod) return 1;
    }
    console.error(a+' '+b);
    return 0;
}

function toStacks(tag) {
    return utils.listOf(Ingredient.of(tag).stacks).toArray();
}

function fluidStack(tag, amount) {
    return {
        tag: tag,
        amount: amount,
        type: 'immersiveengineering:fluid'
    };
}

function parseIntD(str, d) {
    var num = parseInt(str);
    return num != NaN? num : d;
}

function drop(str, sep) {
    var arr = str.split(sep);
    return arr[arr.length-1];
}

function mmItem(isTag, id, count) {
    var build = { type: 'masterfulmachinery:items', data: { count: count } };
    if(isTag) {
        build.data.tag = id;
    }else{
        build.data.item = id;
    }
    return build;
}