import packageJson from '../../package.json' with { type: "json" };
const { version } = packageJson;
/**
 * Manages the loading and unloading of models.
 * Handles communication with the core system or an external service (future support).
 */
export class ModelLoadManager {
    coreInterop;
    externalServiceUrl;
    headers;
    constructor(coreInterop, externalServiceUrl) {
        this.coreInterop = coreInterop;
        this.externalServiceUrl = externalServiceUrl;
        this.headers = {
            'User-Agent': `foundry-local-js-sdk/${version}`
        };
    }
    /**
     * Loads a model into memory.
     * @param modelId - The ID of the model to load.
     * @throws Error - If loading via external service fails.
     */
    async load(modelId) {
        if (this.externalServiceUrl) {
            const url = new URL(`models/load/${encodeURIComponent(modelId)}`, this.externalServiceUrl);
            try {
                const response = await fetch(url.toString(), { headers: this.headers });
                if (!response.ok) {
                    throw new Error(`Error loading model ${modelId} from ${this.externalServiceUrl}: ${response.statusText}`);
                }
            }
            catch (error) {
                throw new Error(`Network error occurred while loading model ${modelId} from ${this.externalServiceUrl}: ${error.message}`);
            }
            return;
        }
        this.coreInterop.executeCommand("load_model", { Params: { Model: modelId } });
    }
    /**
     * Unloads a model from memory.
     * @param modelId - The ID of the model to unload.
     * @throws Error - If unloading via external service fails.
     */
    async unload(modelId) {
        if (this.externalServiceUrl) {
            const url = new URL(`models/unload/${encodeURIComponent(modelId)}`, this.externalServiceUrl);
            const response = await fetch(url.toString(), { headers: this.headers });
            if (!response.ok) {
                throw new Error(`Error unloading model ${modelId} from ${this.externalServiceUrl}: ${response.statusText}`);
            }
            return;
        }
        this.coreInterop.executeCommand("unload_model", { Params: { Model: modelId } });
    }
    /**
     * Lists the IDs of all currently loaded models.
     * @returns An array of loaded model IDs.
     * @throws Error - If listing via external service fails or if JSON parsing fails.
     */
    async listLoaded() {
        if (this.externalServiceUrl) {
            const url = new URL('models/loaded', this.externalServiceUrl);
            const response = await fetch(url.toString(), { headers: this.headers });
            if (!response.ok) {
                throw new Error(`Error listing loaded models from ${this.externalServiceUrl}: ${response.statusText}`);
            }
            const list = await response.json();
            return list || [];
        }
        const response = this.coreInterop.executeCommand("list_loaded_models");
        try {
            return JSON.parse(response);
        }
        catch (error) {
            throw new Error(`Failed to decode JSON response: ${error}. Response was: ${response}`);
        }
    }
}
