import {Note} from "../cards";

export class ToolTips {
    private toolTips: Note[];
    constructor(toolTips: Note[] = []) {
        this.toolTips = toolTips
    }

    add(note: Note): void | true {
        if (this.toolTips.findIndex(t => t.id === note.id) > -1) {
            return
        }

        this.toolTips.push(note)

        return true;
    }

    set(note: Note): void {
        const noteIndex = this.toolTips.findIndex(note => note.id === note.id)

        if (noteIndex > -1) {
            this.toolTips[noteIndex] = note
        } else {
            this.toolTips.push(note)
        }
    }

    update(id: Note['id'], note: Note) {
        const noteIndex = this.toolTips.findIndex(note => note.id === id)

        if (noteIndex > -1) {
            this.toolTips[noteIndex] = note
        } else {
            this.toolTips.push(note)
        }

        // so that the reference is updated for the code that uses references to check for changes
        this.refreshToolTips();
    }

    all() {
        return this.toolTips
    }

    remove(id) {
        const noteIndex = this.toolTips.findIndex(note => note.id === id)

        if (noteIndex > -1) {
            this.toolTips.splice(noteIndex, 1)
        }

        this.refreshToolTips();
    }

    private refreshToolTips = () => {
        this.toolTips = [...this.toolTips]
    }
}