System.register(['react'], (function (exports) {
    'use strict';
    var useRef, React, useLayoutEffect, useEffect;
    return {
        setters: [function (module) {
            useRef = module.useRef;
            React = module["default"];
            useLayoutEffect = module.useLayoutEffect;
            useEffect = module.useEffect;
        }],
        execute: (function () {

            const w = typeof window !== 'undefined' ? window : undefined;
            const TIMEOUT = 3000;
            const uniqueTagsAndElements = (list, regex) => list.reduce((prev, curr) => {
                const unique = !prev.tags.includes(curr.tagName) &&
                    (regex ? curr.tagName.match(regex) : true);
                return unique
                    ? {
                        tags: prev.tags.concat(curr.tagName),
                        elements: prev.elements.concat(curr),
                    }
                    : prev;
            }, {
                tags: [],
                elements: [],
            });
            const initialStyle = { visibility: 'hidden' };
            const setVisibility = (el, on) => {
                if (!el)
                    return;
                if (on)
                    el.removeAttribute('style');
                else
                    el.style.visibility = 'hidden';
            };
            const unique = (...arr) => 
            //@ts-ignore
            Array.from(new Set([].concat.apply([], arr))).sort();
            const merge = (arr1, arr2) => {
                const big = arr1.length >= arr2.length ? arr1 : arr2;
                const small = big === arr1 ? arr2 : arr1;
                return [
                    unique(arr1, arr2),
                    unique(big).filter(x => !unique(small).includes(x)),
                ];
            };
            const createAntiFlicker = exports('createAntiFlicker', ({ checkPredicate = (el) => el.classList.contains('hydrated'), cleanupOnRun = true, observerOptions = { attributes: true, attributeFilter: ['class'] }, onCompleteRun, regex = new RegExp(/(TELIA-|B2(X|C|B)-)(?!VST)/), setContainerStyle = true, styleSetter = setVisibility, timeout = TIMEOUT, } = {}) => {
                let cache = [];
                let delta = [];
                let container;
                let timer;
                let elements;
                const show = (kind) => {
                    onCompleteRun?.(cache, delta, kind);
                    if (setContainerStyle)
                        styleSetter(container, true);
                    // eslint-disable-next-line
                    if (kind === 'timeout')
                        console.warn('Waiting for custom elements to render timed out.');
                    cleanup();
                };
                const check = () => {
                    if (elements.every(checkPredicate))
                        show('success');
                };
                const observer = w && new MutationObserver(check);
                const cleanup = () => {
                    clearTimeout(timer);
                    observer?.disconnect();
                    delta = [];
                };
                return {
                    cleanup,
                    initialStyle,
                    run: _container => {
                        if (cleanupOnRun)
                            cleanup();
                        container = _container;
                        const previousTags = cache;
                        const { tags, elements: _elements } = uniqueTagsAndElements(Array.from(container?.querySelectorAll('*') ?? []), regex);
                        elements = _elements;
                        //@ts-ignore
                        timer = setTimeout(show, timeout, 'timeout');
                        [cache, delta] = merge(previousTags, tags);
                        if (previousTags.length === cache.length) {
                            show('bypass');
                            return cleanup;
                        }
                        if (setContainerStyle)
                            styleSetter(container, false);
                        elements.forEach(el => {
                            observer?.observe(el, observerOptions);
                        });
                        return cleanup;
                    },
                };
            });

            const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
            const AntiFlicker = exports('AntiFlicker', ({ checkPredicate, onCompleteRun, regex, setContainerStyle, styleSetter, timeout, children, style, ...props }) => {
                const instance = useRef();
                const container = useRef(null);
                instance.current ?? (instance.current = createAntiFlicker({
                    checkPredicate,
                    cleanupOnRun: false,
                    regex,
                    onCompleteRun,
                    setContainerStyle,
                    styleSetter,
                    timeout,
                }));
                const { initialStyle, run: antiFlicker } = instance.current;
                useIsomorphicLayoutEffect(() => antiFlicker(container.current));
                return (React.createElement("div", { style: { ...initialStyle, ...style }, ...props, ref: container }, children));
            });

        })
    };
}));
//# sourceMappingURL=index.system.js.map
