import { getContext, setContext, untrack } from 'svelte';
import { MathUtils } from 'three';
const key = Symbol('logs-context');
export const provideLogs = () => {
    const logs = $state([]);
    const warnings = $state([]);
    const errors = $state([]);
    const intl = new Intl.DateTimeFormat('en-US', {
        dateStyle: 'short',
        timeStyle: 'short',
    });
    setContext(key, {
        get current() {
            return logs;
        },
        get errors() {
            return errors;
        },
        get warnings() {
            return warnings;
        },
        add(message, level = 'info') {
            untrack(() => {
                const timestamp = intl.format(Date.now());
                const match = logs.find((log) => log.message === message && log.level === level && log.timestamp === timestamp);
                if (match) {
                    match.count += 1;
                }
                else {
                    const log = {
                        timestamp,
                        message,
                        count: 1,
                        level,
                        uuid: MathUtils.generateUUID(),
                    };
                    logs.unshift(log);
                    if (level === 'error') {
                        errors.unshift(log);
                    }
                    else if (level === 'warn') {
                        warnings.unshift(log);
                    }
                }
                if (logs.length > 200) {
                    const log = logs.pop();
                    if (log && level === 'error') {
                        errors.splice(errors.indexOf(log), 1);
                    }
                    else if (log && level === 'warn') {
                        warnings.splice(errors.indexOf(log), 1);
                    }
                }
            });
        },
    });
};
export const useLogs = () => {
    return getContext(key);
};
