export class AsanaClient {
    accessToken;
    baseUrl = 'https://app.asana.com/api/1.0';
    constructor(accessToken) {
        this.accessToken = accessToken;
    }
    async request(path, init = {}) {
        const url = new URL(`${this.baseUrl}${path}`);
        if (init.query) {
            for (const [key, value] of Object.entries(init.query)) {
                if (value !== undefined) {
                    url.searchParams.set(key, String(value));
                }
            }
        }
        // eslint-disable-next-line n/no-unsupported-features/node-builtins -- fetch is available in supported Node runtimes for this CLI
        const response = await fetch(url, {
            method: init.method ?? 'GET',
            headers: {
                Authorization: `Bearer ${this.accessToken}`,
                'Content-Type': 'application/json',
            },
            body: init.body ? JSON.stringify(init.body) : undefined,
        });
        const payload = await response.json();
        if (!response.ok) {
            const errorMessage = payload?.errors?.[0]?.message ?? `Asana API request failed (${response.status})`;
            throw new Error(errorMessage);
        }
        return payload.data;
    }
    async verify() {
        const user = await this.request('/users/me', {
            query: { opt_fields: 'gid,name,email,workspaces.gid,workspaces.name' },
        });
        return {
            gid: user.gid,
            name: user.name,
            email: user.email,
            workspaces: (user.workspaces ?? []).map((workspace) => ({
                gid: workspace.gid,
                name: workspace.name,
            })),
        };
    }
    async listWorkspaces() {
        const user = await this.verify();
        return user.workspaces;
    }
    async getWorkspace(workspaceGid) {
        try {
            const workspace = await this.request(`/workspaces/${workspaceGid}`);
            return { gid: workspace.gid, name: workspace.name };
        }
        catch {
            return null;
        }
    }
    async listProjects(workspaceGid) {
        const projects = await this.request('/projects', {
            query: {
                workspace: workspaceGid,
                archived: false,
                opt_fields: 'gid,name',
            },
        });
        return projects.map((project) => ({ gid: project.gid, name: project.name }));
    }
    async getProject(projectGid) {
        try {
            const project = await this.request(`/projects/${projectGid}`, {
                query: { opt_fields: 'gid,name' },
            });
            return { gid: project.gid, name: project.name };
        }
        catch {
            return null;
        }
    }
    async getTask(taskGid) {
        try {
            const task = await this.request(`/tasks/${taskGid}`, {
                query: { opt_fields: 'gid,name,completed,notes' },
            });
            return {
                gid: task.gid,
                name: task.name,
                completed: task.completed,
                notes: task.notes,
            };
        }
        catch {
            return null;
        }
    }
    async createTask(input) {
        const task = await this.request('/tasks', {
            method: 'POST',
            body: { data: input },
        });
        return {
            gid: task.gid,
            name: task.name,
            completed: task.completed,
            notes: task.notes,
        };
    }
    async updateTask(taskGid, input) {
        const task = await this.request(`/tasks/${taskGid}`, {
            method: 'PUT',
            body: { data: input },
        });
        return {
            gid: task.gid,
            name: task.name,
            completed: task.completed,
            notes: task.notes,
        };
    }
}
//# sourceMappingURL=client.js.map