var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { InjectVegaTelemetrySlimmer } from "../../../../../vega-slimmer/vega-slimmer-core";
import { WaitForVegaPayloadDefinition } from "../../constants/runtime-metrics-payload-definition/public-api/WaitForVegaHandler/runtime-metrics-payload-definition";
import { createPublicAPIRuntimeMetricsSlimmer } from "../slimmers/runtime-metrics/create-public-api-runtime-metrics-slimmer";
/**
 * WaitForVegaHandler to check whether the vega component has been loaded.
 */
class WaitForVegaHandler {
    constructor() {
        this.vegaPublicAPIRuntimeMetricsSlimmer = createPublicAPIRuntimeMetricsSlimmer(WaitForVegaHandler, ['handle'], WaitForVegaPayloadDefinition);
    }
    /* istanbul ignore next */
    /**
     * Wait for all vega elements to be ready.
     *
     * The below method is e2e-test covered in @see{module:wait-for-vega-should-work-as-expected}
     *
     * @vegaVersion 2.8.0
     */
    async handle() {
        return await Promise.all(
        // eslint-disable-next-line no-restricted-globals
        Array.from(document.querySelectorAll('*:not(.hydrated)'))
            .filter((item) => item.tagName.startsWith('VEGA'))
            .map((item) => { var _a; return (_a = item.componentOnReady) === null || _a === void 0 ? void 0 : _a.call(item); }));
    }
}
__decorate([
    InjectVegaTelemetrySlimmer()
], WaitForVegaHandler.prototype, "vegaPublicAPIRuntimeMetricsSlimmer", void 0);
const WaitForVega = new WaitForVegaHandler();
/* istanbul ignore next */
/**
 * Wait for all vega elements to be ready.
 *
 * The below method is e2e-test covered in @see{module:wait-for-vega-should-work-as-expected}
 *
 * @returns {Promise} A promise that resolves when all vega elements are ready.
 */
export const waitForVega = async () => {
    return WaitForVega.handle();
};
