import { TaskEngineError } from "../transitions.js";
function emptyWishlistDoc() {
    return {
        schemaVersion: 1,
        items: [],
        lastUpdated: new Date().toISOString()
    };
}
export class WishlistStore {
    document;
    persistence;
    constructor(persistence) {
        this.persistence = persistence;
        this.document = emptyWishlistDoc();
    }
    static forSqliteDual(dual) {
        return new WishlistStore({
            pathLabel: `${dual.getDisplayPath()}#wishlist`,
            loadDocument: async () => dual.wishlistDocument,
            saveDocument: async (doc) => {
                dual.seedFromDocuments(dual.taskDocument, doc);
                dual.persistSync();
            }
        });
    }
    async load() {
        this.document = await this.persistence.loadDocument();
    }
    async save() {
        this.document.lastUpdated = new Date().toISOString();
        await this.persistence.saveDocument(this.document);
    }
    getAllItems() {
        return [...this.document.items];
    }
    getItem(id) {
        return this.document.items.find((i) => i.id === id);
    }
    addItem(item) {
        if (this.document.items.some((i) => i.id === item.id)) {
            throw new TaskEngineError("duplicate-task-id", `Wishlist item '${item.id}' already exists`);
        }
        this.document.items.push({ ...item });
    }
    updateItem(item) {
        const idx = this.document.items.findIndex((i) => i.id === item.id);
        if (idx === -1) {
            throw new TaskEngineError("task-not-found", `Wishlist item '${item.id}' not found`);
        }
        this.document.items[idx] = { ...item };
    }
    getFilePath() {
        return this.persistence.pathLabel;
    }
    getLastUpdated() {
        return this.document.lastUpdated;
    }
}
