"use strict";
this.name = "leviathan_market";
this.author = "phkb";
this.copyright = "2026 phkb";
this.license = "CC BY-NC-SA 4.0";

this.$originalDefs = {
    "military": {
        "food": [0, 0, 19, -2, 0, 0, 1, 0, 0],
        "textiles": [0, 0, 20, -1, 0, 0, 3, 0, 0],
        "radioactives": [0, 0, 42, -3, -3, 1, 7, 3, 0],
        "slaves": [0, 0, 40, -5, 0, 0, 31, 0, 0],
        "liquor_wines": [0, 0, 105, -5, 0, 0, 15, 0, 0],
        "luxuries": [0, 0, 236, 8, 0, 0, 3, 0, 0],
        "narcotics": [0, 0, 235, 29, 0, 0, 120, 0, 0],
        "computers": [0, 0, 51, 14, 0, 0, 3, 0, 0],
        "machinery": [0, 0, 117, 6, 0, 0, 7, 0, 0],
        "alloys": [0, 0, 78, 1, 0, 0, 31, 0, 0],
        "firearms": [0, 0, 32, 13, 0, 0, 7, 0, 0],
        "furs": [0, 0, 36, -9, 0, 0, 63, 0, 0],
        "minerals": [0, 0, 16, -1, -1, 85, 3, 3, 0],
        "gold": [0, 0, 73, -1, -1, 5, 7, 3, 1],
        "platinum": [0, 0, 145, -2, -2, 6, 31, 7, 1],
        "gem_stones": [0, 0, 25, -1, -1, 250, 15, 15, 2],
        "alien_items": [0, 0, 22, 15, 0, 0, 7, 0, 0]
    },
    "pirate": {
        "food": [0, 0, 25, -2, 0, 0, 1, 0, 0],
        "textiles": [0, 0, 20, -1, 0, 0, 3, 0, 0],
        "radioactives": [0, 0, 42, -3, -3, 1, 7, 3, 0],
        "slaves": [0, 0, 40, -5, 0, 2, 0, 10, 0],
        "liquor_wines": [0, 0, 160, -5, 0, 0, 15, 0, 0],
        "luxuries": [0, 0, 236, 8, 0, 0, 3, 0, 0],
        "narcotics": [0, 0, 10, 2, 0, 3, 20, 12, 0],
        "computers": [0, 0, 51, 14, 0, 0, 3, 0, 0],
        "machinery": [0, 0, 117, 6, 0, 0, 7, 0, 0],
        "alloys": [0, 0, 78, 1, 0, 0, 31, 0, 0],
        "firearms": [0, 0, 80, 0, 0, 29, 5, 7, 0],
        "furs": [0, 0, 36, -9, 0, 0, 63, 0, 0],
        "minerals": [0, 0, 16, -1, -1, 85, 3, 3, 0],
        "gold": [0, 0, 73, -1, -1, 5, 7, 3, 1],
        "platinum": [0, 0, 145, -2, -2, 6, 31, 7, 1],
        "gem_stones": [0, 0, 25, -1, -1, 250, 15, 15, 2],
        "alien_items": [0, 0, 22, 15, 0, 0, 7, 0, 0]
    },
}

//-------------------------------------------------------------------------------------------------------------
this.updateLocalCommodityDefinition = function (goodDefinition, station) {

    if (!station) return goodDefinition;
	if (!station.hasRole("leviathan") && !station.hasRole("leviathan-pirate")) return goodDefinition;

    var type = "military";
    if (station.hasRole("leviathan-pirate")) type = "pirate"

    var commodity = goodDefinition.key;
    var oldDefs = this.$originalDefs[type][commodity];
    //old style definition found for the good. calculate it the old way
    if (oldDefs) {
        var market_base_price = oldDefs[2];
        var market_eco_adjust_price = oldDefs[3];
        var market_eco_adjust_quantity = oldDefs[4];
        var market_base_quantity = oldDefs[5];
        var market_mask_price = oldDefs[6];
        var market_mask_quantity = oldDefs[7];
        var market_rnd = Math.floor(Math.random() * 256);

        var economy = system.economy;

        var price = (market_base_price + (market_rnd & market_mask_price) + (economy * market_eco_adjust_price)) & 255;
        price *= 0.4;

        var quantity = (market_base_quantity + (market_rnd & market_mask_quantity) - (economy * market_eco_adjust_quantity)) & 255;
        if (quantity > 127) quantity = 0;
        quantity &= 63;

        goodDefinition.price = price * 10;
        goodDefinition.quantity = quantity;
    }
    //no definition found. nullify the goods.
    else {
        goodDefinition.price = 0;
        goodDefinition.quantity = 0;
    }
    return goodDefinition;
}