import { SPARKLE_WORKLET_SOURCE } from './sparkle-worklet.js';
let workletReady = false;
let workletLoading = null;
let propertiesRegistered = false;
let overlay = null;
function registerProperties() {
    if (propertiesRegistered)
        return;
    propertiesRegistered = true;
    const defs = [
        ['--sparkle-progress', '<number>', '0'],
        ['--sparkle-cx', '<number>', '0'],
        ['--sparkle-cy', '<number>', '0'],
        ['--sparkle-size', '<number>', '300'],
    ];
    for (const [name, syntax, initialValue] of defs) {
        try {
            CSS.registerProperty({ name, syntax, inherits: false, initialValue });
        }
        catch {
            // Already registered or unsupported
        }
    }
}
function loadWorklet() {
    if (workletReady)
        return Promise.resolve();
    if (workletLoading)
        return workletLoading;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const paintWorklet = CSS.paintWorklet;
    if (!paintWorklet)
        return Promise.resolve();
    const blob = new Blob([SPARKLE_WORKLET_SOURCE], { type: 'text/javascript' });
    const url = URL.createObjectURL(blob);
    const promise = paintWorklet
        .addModule(url)
        .then(() => {
        workletReady = true;
    })
        .catch(() => { })
        .finally(() => {
        URL.revokeObjectURL(url);
    });
    workletLoading = promise;
    return promise;
}
export function ensureOverlay() {
    if (!overlay) {
        overlay = document.createElement('div');
        overlay.style.cssText = 'position:fixed;inset:0;pointer-events:none;background:transparent;z-index:2147483647;';
        document.body.appendChild(overlay);
    }
    overlay.style.viewTransitionName = 'sparkle-overlay';
}
export function generateSparkleCSS(x, y, size, duration) {
    return `
    ::view-transition-group(sparkle-overlay) {
      z-index: 2147483647;
      background-image: paint(btu-sparkle);
      animation: btu-sparkle-anim ${duration}ms linear forwards;
      --sparkle-cx: ${x};
      --sparkle-cy: ${y};
      --sparkle-size: ${size};
      --sparkle-progress: 0;
    }
    ::view-transition-image-pair(sparkle-overlay) {
      display: none;
    }
    @keyframes btu-sparkle-anim {
      from { --sparkle-progress: 0; }
      to { --sparkle-progress: 1; }
    }
  `;
}
export function cleanupSparkle() {
    if (overlay) {
        overlay.style.viewTransitionName = '';
    }
}
export function isSparkleSupported() {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return !!CSS.paintWorklet;
}
export function isWorkletReady() {
    return workletReady;
}
// Pre-load worklet and register properties at import time
registerProperties();
loadWorklet();
//# sourceMappingURL=sparkle.js.map