/**
 * Workspace API - Direct access to parse, lint, complete, hover.
 *
 * Use this for:
 * - Custom editor integrations
 * - Build-time SQL linting
 * - Simple tooling that doesn't need full LSP
 *
 * @example
 * ```typescript
 * import { createWorkspace } from '@postgres-language-server/wasm/workspace';
 *
 * const workspace = await createWorkspace();
 * workspace.insertFile('/query.sql', 'SELECT * FROM users;');
 * const diagnostics = workspace.lint('/query.sql');
 * ```
 */
import { loadWasm, allocateString, readAndFreeString, parseResult } from "./common.js";
/**
 * The Workspace class provides a direct API for SQL parsing, linting,
 * completions, and hover information.
 */
export class Workspace {
    constructor(module) {
        this.module = module;
    }
    /**
     * Set the database schema from a SchemaCache object or JSON string.
     */
    setSchema(schema) {
        const json = typeof schema === "string" ? schema : JSON.stringify(schema);
        const jsonPtr = allocateString(this.module, json);
        try {
            const resultPtr = this.module._pgls_set_schema(jsonPtr);
            const result = readAndFreeString(this.module, resultPtr);
            if (result !== null) {
                throw new Error(result);
            }
        }
        finally {
            this.module._free(jsonPtr);
        }
    }
    /**
     * Clear the current schema.
     */
    clearSchema() {
        this.module._pgls_clear_schema();
    }
    /**
     * Insert or update a file in the workspace.
     */
    insertFile(path, content) {
        const pathPtr = allocateString(this.module, path);
        const contentPtr = allocateString(this.module, content);
        try {
            const resultPtr = this.module._pgls_insert_file(pathPtr, contentPtr);
            const result = readAndFreeString(this.module, resultPtr);
            if (result !== null) {
                throw new Error(result);
            }
        }
        finally {
            this.module._free(pathPtr);
            this.module._free(contentPtr);
        }
    }
    /**
     * Remove a file from the workspace.
     */
    removeFile(path) {
        const pathPtr = allocateString(this.module, path);
        try {
            this.module._pgls_remove_file(pathPtr);
        }
        finally {
            this.module._free(pathPtr);
        }
    }
    /**
     * Lint a file and return diagnostics.
     */
    lint(path) {
        const pathPtr = allocateString(this.module, path);
        try {
            const resultPtr = this.module._pgls_lint(pathPtr);
            const result = readAndFreeString(this.module, resultPtr);
            return parseResult(result) ?? [];
        }
        finally {
            this.module._free(pathPtr);
        }
    }
    /**
     * Get completions at a position in a file.
     */
    complete(path, offset) {
        const pathPtr = allocateString(this.module, path);
        try {
            const resultPtr = this.module._pgls_complete(pathPtr, offset);
            const result = readAndFreeString(this.module, resultPtr);
            return parseResult(result) ?? [];
        }
        finally {
            this.module._free(pathPtr);
        }
    }
    /**
     * Get hover information at a position in a file.
     */
    hover(path, offset) {
        const pathPtr = allocateString(this.module, path);
        try {
            const resultPtr = this.module._pgls_hover(pathPtr, offset);
            const result = readAndFreeString(this.module, resultPtr);
            if (result === null) {
                return null;
            }
            if (result.startsWith("ERROR:")) {
                throw new Error(result.substring(7).trim());
            }
            return result;
        }
        finally {
            this.module._free(pathPtr);
        }
    }
    /**
     * Parse SQL and return any parse errors.
     */
    parse(sql) {
        const sqlPtr = allocateString(this.module, sql);
        try {
            const resultPtr = this.module._pgls_parse(sqlPtr);
            const result = readAndFreeString(this.module, resultPtr);
            return parseResult(result) ?? [];
        }
        finally {
            this.module._free(sqlPtr);
        }
    }
    /**
     * Get the version of the library.
     */
    version() {
        const resultPtr = this.module._pgls_version();
        return readAndFreeString(this.module, resultPtr) ?? "unknown";
    }
}
/**
 * Create a new Workspace instance.
 */
export async function createWorkspace(options) {
    const module = await loadWasm();
    const workspace = new Workspace(module);
    if (options?.schema) {
        workspace.setSchema(options.schema);
    }
    return workspace;
}
export default createWorkspace;
