"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OnePageWizardSummary = exports.OnePageAdaptableWizard = exports.useOnePageAdaptableWizardContext = exports.OnePageAdaptableWizardContext = exports.FormDescriptionText = exports.SummaryText = exports.SummaryTag = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const react_redux_1 = require("react-redux");
const rebass_1 = require("rebass");
const NamedQueryRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/NamedQueryRedux"));
const SimpleButton_1 = tslib_1.__importDefault(require("../../components/SimpleButton"));
const AdaptableContext_1 = require("../AdaptableContext");
const Tabs_1 = require("../../components/Tabs");
const NamedQueryContext_1 = require("../../components/ExpressionEditor/NamedQueryContext");
const OnePageWizards_1 = require("./OnePageWizards");
const kebabCase_1 = tslib_1.__importDefault(require("lodash/kebabCase"));
const SummaryTag = (props) => (React.createElement(rebass_1.Text, { ...props }));
exports.SummaryTag = SummaryTag;
const SummaryText = (props) => (React.createElement(rebass_1.Text, { fontSize: 2, mb: 3, ...props }));
exports.SummaryText = SummaryText;
const FormDescriptionText = (props) => React.createElement(rebass_1.Text, { fontSize: 2, mt: 1, ...props });
exports.FormDescriptionText = FormDescriptionText;
exports.OnePageAdaptableWizardContext = React.createContext({
    data: null,
    sections: [],
    moduleInfo: null,
    api: null,
    namedQuery: false,
});
function useOnePageAdaptableWizardContext() {
    return React.useContext(exports.OnePageAdaptableWizardContext);
}
exports.useOnePageAdaptableWizardContext = useOnePageAdaptableWizardContext;
const OnePageAdaptableWizard = (props) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const dispatch = (0, react_redux_1.useDispatch)();
    const saveNamedQuery = React.useCallback((namedQuery) => dispatch(NamedQueryRedux.NamedQueryAdd(namedQuery)), []);
    const [namedQuery, setNamedQuery] = (0, react_1.useState)(false);
    const name = props.moduleInfo.FriendlyName
        ? `${(0, kebabCase_1.default)(props.moduleInfo.FriendlyName)}-wizard`
        : '';
    const extraContext = {
        data: props.data,
        api,
        namedQuery,
        moduleInfo: props.moduleInfo,
        sections: props.sections,
    };
    const sections = props.sections.map((section) => {
        if (section === '-') {
            return '-';
        }
        const isValid = typeof section.isValid === 'function'
            ? (data, innerContext) => {
                return section.isValid(data, api, {
                    ...innerContext,
                    ...extraContext,
                });
            }
            : void 0;
        const isVisible = typeof section.isVisible === 'function'
            ? (data, innerContext) => {
                return section.isVisible(data, api, {
                    ...innerContext,
                    ...extraContext,
                });
            }
            : void 0;
        return {
            ...section,
            isValid,
            isVisible,
        };
    });
    const handleClickFinish = () => {
        if (namedQuery && namedQuery.Name) {
            saveNamedQuery(namedQuery);
        }
        props.onFinish?.(props.data);
    };
    return (React.createElement(exports.OnePageAdaptableWizardContext.Provider, { value: extraContext },
        React.createElement(NamedQueryContext_1.NamedQueryContext.Provider, { value: { namedQuery, setNamedQuery } },
            React.createElement(OnePageWizards_1.OnePageWizard, { ...props, name: name, sections: sections, onFinish: handleClickFinish }))));
};
exports.OnePageAdaptableWizard = OnePageAdaptableWizard;
const OnePageWizardSummary = () => {
    const { sections, data, api, moduleInfo } = useOnePageAdaptableWizardContext();
    const { setCurrentSection } = (0, OnePageWizards_1.useOnePageWizardContext)();
    const [namedQuery] = (0, react_1.useState)(false);
    const renderEdit = (index) => (React.createElement(SimpleButton_1.default, { px: 1, variant: "text", style: {
            textDecoration: 'underline',
            display: 'inline-block',
            fontSize: 'var(--ab-font-size-2)',
        }, onClick: () => {
            setCurrentSection(index);
        } }, "edit"));
    const extraContext = {
        data,
        api,
        namedQuery,
        moduleInfo: moduleInfo,
        sections,
    };
    return (React.createElement(rebass_1.Box, { className: "ab-OnePageWizardSummary" }, sections
        .map((section, index) => {
        if (section === '-') {
            return null;
        }
        if (!section.renderSummary) {
            return null;
        }
        if (section.isVisible && !section.isVisible(data, api, extraContext)) {
            return null;
        }
        return (React.createElement(Tabs_1.Tabs, { keyboardNavigation: false, mt: index ? 2 : 0, key: index },
            React.createElement(Tabs_1.Tabs.Tab, null,
                section.title,
                " ",
                renderEdit(index)),
            React.createElement(Tabs_1.Tabs.Content, null, section.renderSummary(data, api))));
    })
        .filter(Boolean)));
};
exports.OnePageWizardSummary = OnePageWizardSummary;
