"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Iframe = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const ContentState_1 = require("../ContentState");
const constants_1 = require("./constants");
const styles_1 = require("./styles");
exports.Iframe = (0, react_1.forwardRef)(({ src, loadingContent, onLoading, onError, sandbox = constants_1.DEFAULT_SANDBOX_POLICY, title, }, ref) => {
    const [isLoading, setIsLoading] = (0, react_1.useState)(true);
    const [isError, setIsError] = (0, react_1.useState)(false);
    const [errorMessage, setErrorMessage] = (0, react_1.useState)('');
    const startRefetch = () => {
        setIsError(false);
        setIsLoading(true);
    };
    const handleErrorIframe = (event) => {
        setErrorMessage(event.nativeEvent?.message);
        setIsError(true);
        onError?.(event);
    };
    const handleEndLoadingIframe = (event) => {
        setIsLoading(false);
        onLoading?.(event);
    };
    return ((0, jsx_runtime_1.jsxs)(styles_1.Wrapper, { children: [(0, jsx_runtime_1.jsx)(ContentState_1.ContentState, { isLoading: isLoading, isError: isError, loadingContent: loadingContent, errorState: {
                    errorList: [errorMessage || ''],
                    imgAlt: 'Произошла ошибка',
                    onRetry: startRefetch,
                }, children: (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}) }), !isError && ((0, jsx_runtime_1.jsx)(styles_1.StyledIframe, { src: src, ref: ref, hidden: isLoading, sandbox: sandbox || undefined, onLoad: handleEndLoadingIframe, onError: handleErrorIframe, onErrorCapture: handleErrorIframe, title: title }))] }));
});
