/**
 * WorkspaceClient
 *
 * Client for workspace and project management API
 */
export class WorkspaceClient {
    constructor(config, tenantId = "default") {
        this.baseURL = config.baseURL;
        this.headers = {
            "Content-Type": "application/json",
            Authorization: `Bearer ${config.apiKey}`,
            "x-tenant-id": tenantId,
            ...config.headers,
        };
    }
    async request(url, options = {}) {
        const fullUrl = `${this.baseURL}${url}`;
        const response = await fetch(fullUrl, {
            ...options,
            headers: {
                ...this.headers,
                ...options.headers,
            },
        });
        if (!response.ok) {
            const err = await response.json().catch(() => ({}));
            throw new Error(err.message || `HTTP error! Status: ${response.status}`);
        }
        return response.json();
    }
    // ==================== Workspace CRUD ====================
    async listWorkspaces() {
        const response = await this.request("/api/workspaces");
        return response.data || [];
    }
    async createWorkspace(data) {
        const response = await this.request("/api/workspaces", {
            method: "POST",
            body: JSON.stringify(data),
        });
        return response.data;
    }
    async getWorkspace(workspaceId) {
        const response = await this.request(`/api/workspaces/${workspaceId}`);
        return response.data;
    }
    async updateWorkspace(workspaceId, updates) {
        const response = await this.request(`/api/workspaces/${workspaceId}`, {
            method: "PATCH",
            body: JSON.stringify(updates),
        });
        return response.data;
    }
    async deleteWorkspace(workspaceId) {
        await this.request(`/api/workspaces/${workspaceId}`, {
            method: "DELETE",
        });
    }
    // ==================== Project CRUD ====================
    async listProjects(workspaceId) {
        const response = await this.request(`/api/workspaces/${workspaceId}/projects`);
        return response.data || [];
    }
    async createProject(workspaceId, data) {
        const response = await this.request(`/api/workspaces/${workspaceId}/projects`, {
            method: "POST",
            body: JSON.stringify(data),
        });
        return response.data;
    }
    async getProject(workspaceId, projectId) {
        const response = await this.request(`/api/workspaces/${workspaceId}/projects/${projectId}`);
        return response.data;
    }
    async updateProject(workspaceId, projectId, updates) {
        const response = await this.request(`/api/workspaces/${workspaceId}/projects/${projectId}`, {
            method: "PATCH",
            body: JSON.stringify(updates),
        });
        return response.data;
    }
    async deleteProject(workspaceId, projectId) {
        await this.request(`/api/workspaces/${workspaceId}/projects/${projectId}`, {
            method: "DELETE",
        });
    }
    // ==================== File Operations ====================
    async listPath(workspaceId, projectId, path = "/") {
        const response = await this.request(`/api/workspaces/${workspaceId}/projects/${projectId}/listpath?path=${encodeURIComponent(path)}`);
        return response.data || [];
    }
    async readFile(workspaceId, projectId, path, offset = 0, limit = 1000) {
        const params = new URLSearchParams({
            path,
            offset: String(offset),
            limit: String(limit),
        });
        const response = await this.request(`/api/workspaces/${workspaceId}/projects/${projectId}/readfile?${params}`);
        return response.data;
    }
    /**
     * Upload a file to the workspace project storage.
     *
     * Uses multipart/form-data against:
     * POST /api/workspaces/:workspaceId/projects/:projectId/uploadfile
     */
    async uploadFile(workspaceId, projectId, file, path = "/") {
        const url = `/api/workspaces/${workspaceId}/projects/${projectId}/uploadfile`;
        const fullUrl = `${this.baseURL}${url}`;
        const formData = new FormData();
        const filename = file.name && typeof file.name === "string"
            ? file.name
            : "file";
        formData.append("file", file, filename);
        formData.append("path", path);
        const headers = { ...this.headers };
        // Let the browser set the correct Content-Type with boundary
        delete headers["Content-Type"];
        const response = await fetch(fullUrl, {
            method: "POST",
            headers,
            body: formData,
        });
        if (!response.ok) {
            const err = (await response.json().catch(() => ({})));
            throw new Error(err.error || err.message || `HTTP error! Status: ${response.status}`);
        }
        const json = (await response.json());
        if (!json.success || !json.data) {
            throw new Error(json.error || "Upload failed");
        }
        return json.data;
    }
    getFileDownloadUrl(workspaceId, projectId, filePath) {
        return `${this.baseURL}/api/workspaces/${workspaceId}/projects/${projectId}/downloadfile?path=${encodeURIComponent(filePath)}`;
    }
}
//# sourceMappingURL=workspace-client.js.map