import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useRef } from 'react';
import { styled } from '@mui/material/styles';
import { Box } from '@mui/material';
import { useIsComponentMountedRef, useSCFetchCustomAdv } from '@selfcommunity/react-core';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import HiddenPlaceholder from '../../shared/HiddenPlaceholder';
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
import useResizeObserver from 'use-resize-observer';
import { PREFIX } from './constants';
const classes = {
    root: `${PREFIX}-root`,
    wrap: `${PREFIX}-wrap`,
    image: `${PREFIX}-image`,
    embedCode: `${PREFIX}-embed-code`,
    prefixedHeight: `${PREFIX}-prefixed-height`
};
const Root = styled(Box, {
    name: PREFIX,
    slot: 'Root'
})(() => ({}));
/**
 * > API documentation for the Community-JS CustomAdv component. Learn about the available props and the CSS API.
 *
 *
 * This component renders custom adv banners.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/CustomAdv)

 #### Import
 ```jsx
 import {CustomAdv} from '@selfcommunity/react-ui';
 ```
 #### Component Name
 The name `SCCustomAdv` can be used when providing style overrides in the theme.

 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCCustomAdv-root|Styles applied to the root element.|
 |wrap|.SCCustomAdv-wrap|Styles applied to wrap an element.|
 |image|.SCCustomAdv-image|Styles applied to the image element.|
 |embedCode|.SCCustomAdv-embed-code|Styles applied to the embed code section.|
 |prefixedHeight|.SCCustomAdv-prefixed-height|Styles applied to handle a prefixed height.|

 * @param inProps
 */
export default function CustomAdv(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { id = 'custom_adv', className, advId = null, position, categoriesId, prefixedHeight, onStateChange, onHeightChange } = props;
    // REFS
    const isMountedRef = useIsComponentMountedRef();
    const estimatedHeight = useRef(0);
    // ADV
    const { scCustomAdv } = useSCFetchCustomAdv({ id: advId, position, categoriesId });
    /**
     * Virtual Feed update
     */
    useDeepCompareEffectNoCheck(() => {
        if (scCustomAdv) {
            onStateChange && onStateChange({ advId: scCustomAdv.id, prefixedHeight: estimatedHeight.current });
        }
        onHeightChange && onHeightChange();
    }, [scCustomAdv]);
    /**
     * Use useResizeObserver to intercept layout change:
     * onResize callback function, receive the width and height of the
     * element when it changes and call onHeightChange
     */
    const { ref } = useResizeObserver({
        round: (n) => {
            return n;
        },
        onResize: ({ width, height }) => {
            if (isMountedRef.current) {
                estimatedHeight.current = height;
                onStateChange && onStateChange({ advId: scCustomAdv.id, prefixedHeight: height });
            }
        }
    });
    const embedRef = useRef(null);
    useEffect(() => {
        const embedCode = scCustomAdv === null || scCustomAdv === void 0 ? void 0 : scCustomAdv.embed_code;
        const container = embedRef.current;
        if (container && embedCode) {
            // Create a temporary container for parsing HTML
            const tempDiv = document.createElement('div');
            tempDiv.innerHTML = embedCode;
            // Extract scripts from HTML content
            const scripts = tempDiv.getElementsByTagName('script');
            const scriptContents = [];
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            for (let script of scripts) {
                if (script.src) {
                    // If the script has a src attribute, create a new script element and set its src
                    const newScript = document.createElement('script');
                    newScript.src = script.src;
                    document.body.appendChild(newScript);
                }
                else {
                    // If the script contains inline code, store its content for later execution
                    scriptContents.push(script.innerHTML);
                }
                // Remove the script tag from the tempDiv
                script.parentNode.removeChild(script);
            }
            // Insert the HTML content into the container
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            container.innerHTML = tempDiv.innerHTML;
            // Execute inline script contents
            scriptContents.forEach((scriptContent) => {
                try {
                    const newScript = document.createElement('script');
                    newScript.appendChild(document.createTextNode(scriptContent));
                    document.body.appendChild(newScript);
                }
                catch (e) {
                    console.error('Error executing script', e);
                }
            });
        }
    }, [scCustomAdv]);
    if (!scCustomAdv) {
        return _jsx(HiddenPlaceholder, {});
    }
    const adv = (_jsxs(Box, Object.assign({ className: classes.wrap }, { children: [scCustomAdv.image && (_jsx("img", { src: scCustomAdv.image, alt: scCustomAdv.title, className: classNames(classes.image, { [classes.prefixedHeight]: Boolean(prefixedHeight) }) })), scCustomAdv.embed_code && (_jsx(Box, { ref: embedRef, className: classNames(classes.embedCode, { [classes.prefixedHeight]: Boolean(prefixedHeight) }), dangerouslySetInnerHTML: { __html: scCustomAdv.embed_code } }))] })));
    return (_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className), ref: ref, style: Object.assign({}, (prefixedHeight ? { paddingBottom: prefixedHeight } : { width: '100%' })) }, { children: scCustomAdv.link ? (_jsx("a", Object.assign({ href: scCustomAdv.link, title: scCustomAdv.title }, { children: adv }))) : (adv) })));
}
