export const SYNCULAR_CLIENT_PACKAGE_NAME = '@syncular/client';
export const SYNCULAR_CLIENT_PACKAGE_VERSION = '0.1.1';
export const SYNCULAR_WASM_OUT_NAME = 'syncular';
export const SYNCULAR_WASM_GLUE_FILE = `${SYNCULAR_WASM_OUT_NAME}.js`;
export const SYNCULAR_WASM_BINARY_FILE = `${SYNCULAR_WASM_OUT_NAME}_bg.wasm`;
export const SYNCULAR_WASM_ARTIFACT_FILE = 'syncular-runtime-artifact.json';
export const SYNCULAR_WASM_ARTIFACT_CATALOG_FILE = 'syncular-runtime-artifacts.json';
export const SYNCULAR_FULL_RUNTIME_FEATURES = [
    'web-owned-sqlite-core',
    'web-owned-sqlite',
    'blobs',
    'crdt-yjs',
    'e2ee',
];
export const SYNCULAR_CORE_RUNTIME_FEATURES = [
    'web-owned-sqlite-core',
];
export function getSyncularWasmGlueUrl() {
    return resolveSyncularWasmAsset(SYNCULAR_WASM_GLUE_FILE);
}
export function getSyncularWasmUrl() {
    return resolveSyncularWasmAsset(SYNCULAR_WASM_BINARY_FILE);
}
export function getSyncularRuntimeArtifactCatalogUrl() {
    const runtimeUrl = new URL(import.meta.url);
    const sourceRuntime = runtimeUrl.pathname.endsWith('/src/wasm-bindings/runtime-contract.ts');
    return new URL(sourceRuntime
        ? `../../dist/${SYNCULAR_WASM_ARTIFACT_CATALOG_FILE}`
        : `../${SYNCULAR_WASM_ARTIFACT_CATALOG_FILE}`, runtimeUrl);
}
export function getSyncularRuntimeArtifact(variant = 'full') {
    const dir = variant === 'core'
        ? 'wasm-core'
        : variant === 'full-perf'
            ? 'wasm-perf'
            : 'wasm';
    const features = variant === 'core'
        ? SYNCULAR_CORE_RUNTIME_FEATURES
        : SYNCULAR_FULL_RUNTIME_FEATURES;
    return {
        name: variant,
        features,
        wasmGlueUrl: resolveSyncularWasmAsset(SYNCULAR_WASM_GLUE_FILE, dir),
        wasmUrl: resolveSyncularWasmAsset(SYNCULAR_WASM_BINARY_FILE, dir),
    };
}
export function getSyncularPackagedRuntimeArtifacts() {
    return [
        getSyncularRuntimeArtifact('core'),
        getSyncularRuntimeArtifact('full'),
        getSyncularRuntimeArtifact('full-perf'),
    ];
}
export function resolveSyncularRuntimeArtifactCatalog(catalog, options = {}) {
    const baseUrl = options.baseUrl ?? getSyncularRuntimeArtifactCatalogUrl();
    return catalog.artifacts.map((artifact) => ({
        name: artifact.name,
        features: artifact.features,
        wasmGlueUrl: resolveCatalogAssetUrl(artifact.wasmGlueUrl, baseUrl),
        wasmUrl: resolveCatalogAssetUrl(artifact.wasmUrl, baseUrl),
    }));
}
export function selectSyncularRuntimeArtifact(requiredFeatures = [], artifacts = [
    getSyncularRuntimeArtifact('full'),
]) {
    const required = new Set(requiredFeatures);
    for (const artifact of artifacts) {
        const available = new Set(artifact.features);
        let compatible = true;
        for (const feature of required) {
            if (!available.has(feature)) {
                compatible = false;
                break;
            }
        }
        if (compatible) {
            return {
                wasmGlueUrl: artifact.wasmGlueUrl,
                wasmUrl: artifact.wasmUrl,
            };
        }
    }
    throw new Error(`No Syncular Rust runtime artifact satisfies required features: ${[
        ...required,
    ].join(', ')}`);
}
function resolveSyncularWasmAsset(fileName, dir = 'wasm') {
    const runtimeUrl = new URL(import.meta.url);
    const sourceRuntime = runtimeUrl.pathname.endsWith('/src/wasm-bindings/runtime-contract.ts');
    return new URL(sourceRuntime ? `../../dist/${dir}/${fileName}` : `../${dir}/${fileName}`, runtimeUrl);
}
function resolveCatalogAssetUrl(value, baseUrl) {
    if (isAbsoluteAssetUrl(value))
        return value;
    if (baseUrl instanceof URL) {
        return new URL(value, new URL('./', baseUrl));
    }
    if (hasUrlProtocol(baseUrl)) {
        return new URL(value, new URL('./', baseUrl)).href;
    }
    const baseDir = baseUrl.endsWith('/')
        ? baseUrl
        : baseUrl.slice(0, Math.max(0, baseUrl.lastIndexOf('/') + 1));
    return `${baseDir}${value}`;
}
function isAbsoluteAssetUrl(value) {
    return value.startsWith('/') || hasUrlProtocol(value);
}
function hasUrlProtocol(value) {
    return /^[a-z][a-z0-9+.-]*:/i.test(value);
}
//# sourceMappingURL=runtime-contract.js.map