export class GlobalState {
    constructor() {
        this.playerData = {
            hearts: 3,
            maxHearts: 3,
            score: 0,
            inventory: [],
            upgrades: [],
            startGulchX: 950,
            startGulchY: 4250,
            startNNX: 2400,
            startNNY: 3350,
            startProX: 350,
            startProY: 250,
        };
    }

    get hearts() {
        return this.playerData.hearts;
    }

    set hearts(value) {
        if (value < 0) throw new Error("Hearts cannot be less than 0");
        this.playerData.hearts = value;
    }

    get maxHearts() {
        return this.playerData.maxHearts;
    }

    set maxHearts(value) {
        if (value < 0) throw new Error("Max hearts cannot be less than 0");
        this.playerData.maxHearts = value;
    }

    get score() {
        return this.playerData.score;
    }

    set score(value) {
        if (value < 0) throw new Error("Score cannot be less than 0");
        this.playerData.score = value;
    }

    get inventory() {
        return [...this.playerData.inventory]; // Return a copy
    }

    set inventory(value) {
        if (!Array.isArray(value)) throw new Error("Inventory must be an array");
        this.playerData.inventory = value;
    }

    // Add item(s) to inventory (handles positive counts only)
    addToInventory(itemName, count = 1) {
        if (count < 0) throw new Error("Cannot add negative count to inventory");
        if (count === 0) return;
        
        // Create new array with existing items + added items
        this.inventory = [...this.inventory, ...Array(count).fill(itemName)];
    }

    // Return count of item in inventory (including 0 for missing items)
    hasInventory(itemName) {
        return this.inventory.filter(item => item === itemName).length;
    }

    get upgrades() {
        return [...this.playerData.upgrades]; // Return a copy
    }

    set upgrades(value) {
        if (!Array.isArray(value)) throw new Error("Upgrades must be an array");
        this.playerData.upgrades = value;
    }

    get startGulchX() {
        return this.playerData.startGulchX;
    }

    set startGulchX(value) {
        this.playerData.startGulchX = value;
    }

    get startGulchY() {
        return this.playerData.startGulchY;
    }

    set startGulchY(value) {
        this.playerData.startGulchY = value;
    }

    set startNNX(value) {
        this.playerData.startNNX = value;
    }

    get startNNX() {
        return this.playerData.startNNX;
    }

    set startNNY(value) {
        this.playerData.startNNY = value;
    }

    get startNNY() {
        return this.playerData.startNNY;
    }

    set startProX(value) {
        this.playerData.startProX = value;
    }

    get startProX() {
        return this.playerData.startProX;
    }

    set startProY(value) {
        this.playerData.startProY = value;
    }

    get startProY() {
        return this.playerData.startProY;
    }

    // Add safe score increment method (replaces manual score updates)
    addScore(amount) {
        if (amount < 0) throw new Error("Cannot add negative score");
        this.score += amount;
    }
}