export class TrelloClient {
    apiKey;
    apiToken;
    baseUrl = 'https://api.trello.com/1';
    constructor(apiKey, apiToken) {
        this.apiKey = apiKey;
        this.apiToken = apiToken;
    }
    async request(path, init = {}) {
        const url = new URL(`${this.baseUrl}${path}`);
        url.searchParams.set('key', this.apiKey);
        url.searchParams.set('token', this.apiToken);
        if (init.query) {
            for (const [key, value] of Object.entries(init.query)) {
                if (value !== undefined) {
                    url.searchParams.set(key, String(value));
                }
            }
        }
        const response = await fetch(url, {
            method: init.method ?? 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
            body: init.body ? JSON.stringify(init.body) : undefined,
        });
        if (response.status === 401) {
            throw new Error('Trello authentication failed. Verify your API key and token.');
        }
        if (response.status === 429) {
            throw new Error('Trello API rate limit exceeded. Wait a moment and try again.');
        }
        if (!response.ok) {
            const text = await response.text().catch(() => '');
            throw new Error(`Trello API request failed (${response.status}): ${text}`);
        }
        return response.json();
    }
    async verify() {
        return this.request('/members/me', {
            query: { fields: 'id,fullName,username' },
        });
    }
    async getCard(cardId) {
        try {
            return await this.request(`/cards/${cardId}`, {
                query: { fields: 'id,name,desc,url,shortUrl,idBoard,idList,idMembers,closed,due,dueComplete', members: true, member_fields: 'fullName,username', labels: 'all' },
            });
        }
        catch {
            return null;
        }
    }
    async searchCards(query, options) {
        const limit = Math.max(1, Math.min(options?.limit ?? 20, 100));
        const result = await this.request('/search', {
            query: {
                query,
                modelTypes: 'cards',
                cards_limit: limit,
                card_fields: 'id,name,desc,url,shortUrl,idBoard,idList,idMembers,closed,due,dueComplete',
                card_members: true,
                card_member_fields: 'fullName,username',
                card_labels: 'all',
                ...(options?.boardId ? { idBoards: options.boardId } : {}),
            },
        });
        return result.cards ?? [];
    }
    async getBoard(boardId) {
        try {
            return await this.request(`/boards/${boardId}`, {
                query: { fields: 'id,name,url' },
            });
        }
        catch {
            return null;
        }
    }
    async getBoardLists(boardId) {
        return this.request(`/boards/${boardId}/lists`, {
            query: { fields: 'id,name' },
        });
    }
    async getBoardCards(boardId, options) {
        const filter = options?.filter ?? 'open';
        return this.request(`/boards/${boardId}/cards/${filter}`, {
            query: {
                fields: 'id,name,desc,url,shortUrl,idBoard,idList,idMembers,closed,due,dueComplete',
                members: true,
                member_fields: 'fullName,username',
                labels: 'all',
            },
        });
    }
    async getBoards() {
        return this.request('/members/me/boards', {
            query: { fields: 'id,name,url', filter: 'open' },
        });
    }
    async createCard(input) {
        return this.request('/cards', {
            method: 'POST',
            body: input,
        });
    }
    async updateCard(cardId, input) {
        return this.request(`/cards/${cardId}`, {
            method: 'PUT',
            body: input,
        });
    }
}
//# sourceMappingURL=client.js.map