var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import React, { useState } from "react";
import useEffectOnce from "~/hooks/useEffectOnce";
import errorTracker from "~/utils/errorTracker";
export const ValidateOptions = ({ sdk, schema, options, children, }) => {
    const [validated, setValidated] = useState(false);
    const [validationErrors, setValidationErrors] = useState([]);
    useEffectOnce(() => {
        const validate = () => __awaiter(void 0, void 0, void 0, function* () {
            try {
                errorTracker.addBreadCrumb("ValidateOptions", "Validating state options", {
                    options,
                });
                yield schema.validate(options, { abortEarly: false });
                setValidated(true);
            }
            catch (error) {
                const validationError = error;
                setValidationErrors(validationError.errors);
                errorTracker.addBreadCrumb("ValidateOptions", "State option validation failed", {
                    errors: validationError.errors,
                });
            }
        });
        if (!validated) {
            validate();
        }
    });
    if (!validated) {
        if (validationErrors.length) {
            const error = `Could not load ${sdk}`;
            console.error(`${error}: ${validationErrors.join(", ")}`);
            return React.createElement("p", { "data-testid": "error-message" }, error);
        }
        return React.createElement(React.Fragment, null);
    }
    return children;
};
//# sourceMappingURL=index.js.map