export class Storage {
    constructor() {
        this.signalants = [];
    }

    addSignalant(signalant) {
        if (!signalant || this.signalants.includes(signalant)) {
            return;
        }
        this.signalants.push(signalant);
        console.log(`Added signalant ${signalant.id} to storage. Total stored: ${this.signalants.length}`);
    }

    takeSignalantAt(index) {
        if (index < 0 || index >= this.signalants.length) {
            return null;
        }

        return this.signalants.splice(index, 1)[0] || null;
    }

    removeSignalant(signalant) {
        const index = this.signalants.indexOf(signalant);
        if (index === -1) {
            return false;
        }

        this.signalants.splice(index, 1);
        return true;
    }
}

export const storage = new Storage();

export function ensureTutorialSignalant() {
    if (storage.signalants.some(signalant => signalant?.id === "tutorial_signalant")) {
        return false;
    }

    return import("./signalant.js")
        .then(({ Signalant }) => {
            if (storage.signalants.some(signalant => signalant?.id === "tutorial_signalant")) {
                return false;
            }

            const tutorialSignalant = new Signalant("tutorial_signalant", {
                toStorage: true,
                direction: 0,
                isFixed: false,
                isActive: false,
                affectsColors: ["red", "red", "red"],
                affectedColorsDirections: {
                    red: [40, 0, 320]
                }
            });

            storage.addSignalant(tutorialSignalant);
            return true;
        })
        .catch(() => false);
}