import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { forwardRef, useState, } from 'react';
import { ContentState } from '../ContentState';
import { DEFAULT_SANDBOX_POLICY } from './constants';
import { StyledIframe, Wrapper } from './styles';
export const Iframe = forwardRef(({ src, loadingContent, onLoading, onError, sandbox = DEFAULT_SANDBOX_POLICY, title, }, ref) => {
    const [isLoading, setIsLoading] = useState(true);
    const [isError, setIsError] = useState(false);
    const [errorMessage, setErrorMessage] = 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 (_jsxs(Wrapper, { children: [_jsx(ContentState, { isLoading: isLoading, isError: isError, loadingContent: loadingContent, errorState: {
                    errorList: [errorMessage || ''],
                    imgAlt: 'Произошла ошибка',
                    onRetry: startRefetch,
                }, children: _jsx(_Fragment, {}) }), !isError && (_jsx(StyledIframe, { src: src, ref: ref, hidden: isLoading, sandbox: sandbox || undefined, onLoad: handleEndLoadingIframe, onError: handleErrorIframe, onErrorCapture: handleErrorIframe, title: title }))] }));
});
