"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const grid_1 = require("../../gravity-blocks/grid/index.js");
const sub_blocks_1 = require("../../sub-blocks/index.js");
const utils_1 = require("../../utils/index.js");
const microdata_1 = require("../../utils/microdata.js");
const QuestionBlockItem_1 = require("./QuestionBlockItem/QuestionBlockItem.js");
const b = (0, utils_1.block)('QuestionsBlock');
const QuestionsBlock = (props) => {
    const { title, text, additionalInfo, links, buttons, items, list } = props;
    const [opened, setOpened] = React.useState([0]);
    const toggleItem = (index, itemOnClick) => {
        let newState;
        if (opened.includes(index)) {
            newState = opened.filter((itemIndex) => itemIndex !== index);
        }
        else {
            newState = [...opened, index];
        }
        if (itemOnClick) {
            itemOnClick(index, !opened.includes(index));
        }
        setOpened(newState);
    };
    const faqMicrodataScript = React.useMemo(() => {
        try {
            const json = JSON.stringify({
                '@context': 'https://schema.org',
                '@type': 'FAQPage',
                mainEntity: items.map((item) => ({
                    '@type': 'Question',
                    name: (0, microdata_1.sanitizeMicrodata)(item.title),
                    acceptedAnswer: {
                        '@type': 'Answer',
                        text: (0, microdata_1.sanitizeMicrodata)(item.text),
                    },
                })),
            });
            return (0, jsx_runtime_1.jsx)("script", { type: "application/ld+json", dangerouslySetInnerHTML: { __html: json } });
        }
        catch (error) {
            /*eslint-disable no-console */
            console.warn('Problem with FAQ microdata', error);
            return null;
        }
    }, [items]);
    return ((0, jsx_runtime_1.jsxs)("div", { className: b(), children: [faqMicrodataScript, (0, jsx_runtime_1.jsx)(grid_1.Grid, { children: (0, jsx_runtime_1.jsxs)(grid_1.Row, { children: [(0, jsx_runtime_1.jsx)(grid_1.Col, { sizes: { all: 12, md: 4 }, children: (0, jsx_runtime_1.jsx)("div", { className: b('title'), children: (0, jsx_runtime_1.jsx)(sub_blocks_1.Content, { title: title, text: text, additionalInfo: additionalInfo, links: links, list: list, buttons: buttons, colSizes: { all: 12, md: 12 } }) }) }), (0, jsx_runtime_1.jsx)(grid_1.Col, { sizes: { all: 12, md: 8 }, role: 'list', children: items.map(({ title: itemTitle, text: itemText, link, listStyle = 'dash', onClick: itemOnClick, }, index) => {
                                const isOpened = opened.includes(index);
                                const onClick = () => toggleItem(index, itemOnClick);
                                return ((0, jsx_runtime_1.jsx)(QuestionBlockItem_1.QuestionBlockItem, { title: itemTitle, text: itemText, link: link, listStyle: listStyle, isOpened: isOpened, onClick: onClick }, itemTitle));
                            }) })] }) })] }));
};
exports.default = QuestionsBlock;
//# sourceMappingURL=Questions.js.map
