import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
const EMPTY_STATE = {
    pairedChatId: null,
    pairedAt: null
};
export class WhatsAppBindingStore {
    filePath;
    constructor(filePath) {
        this.filePath = filePath;
    }
    async load() {
        try {
            const raw = await readFile(this.filePath, 'utf8');
            const parsed = JSON.parse(raw);
            return {
                pairedChatId: typeof parsed.pairedChatId === 'string' && parsed.pairedChatId.trim().length > 0
                    ? parsed.pairedChatId
                    : null,
                pairedAt: typeof parsed.pairedAt === 'string' ? parsed.pairedAt : null
            };
        }
        catch {
            return { ...EMPTY_STATE };
        }
    }
    async save(chatId) {
        const nextState = {
            pairedChatId: chatId,
            pairedAt: new Date().toISOString()
        };
        await mkdir(dirname(this.filePath), { recursive: true });
        await writeFile(this.filePath, JSON.stringify(nextState, null, 2), 'utf8');
        return nextState;
    }
    async clear() {
        await mkdir(dirname(this.filePath), { recursive: true });
        await writeFile(this.filePath, JSON.stringify(EMPTY_STATE, null, 2), 'utf8');
    }
}
//# sourceMappingURL=whatsapp-binding-store.js.map