"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShortcutWizard = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const OnePageAdaptableWizard_1 = require("../../../View/Wizard/OnePageAdaptableWizard");
const Helper_1 = require("../../../Utilities/Helpers/Helper");
const NewScopeComponent_1 = require("../../Components/NewScopeComponent");
const ShortcutScopeWizardSection_1 = require("./ShortcutScopeWizardSection");
const ShortcutSettingsWizard_1 = require("./ShortcutSettingsWizard");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../../Utilities/ObjectFactory"));
const react_redux_1 = require("react-redux");
const ShortcutRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/ShortcutRedux"));
const shortcutKeys_1 = require("../shortcutKeys");
const AdaptableContext_1 = require("../../AdaptableContext");
const ObjectTagsWizardSection_1 = require("../../Wizard/ObjectTagsWizardSection");
const Flex_1 = require("../../../components/Flex");
const StringExtensions_1 = tslib_1.__importDefault(require("../../../Utilities/Extensions/StringExtensions"));
const ShortcutWizard = (props) => {
    const [shortcut, doSetShortcut] = React.useState(() => {
        const shortcut = props.data ? (0, Helper_1.cloneObject)(props.data) : ObjectFactory_1.default.CreateEmptyShortcut();
        shortcut.Scope = shortcut.Scope ?? { All: true };
        if (props.popupParams?.column && props.popupParams?.action === 'New') {
            shortcut.Scope = {
                ColumnIds: [props.popupParams.column.columnId],
            };
        }
        return shortcut;
    });
    const setShortcut = React.useCallback((data) => {
        doSetShortcut(data);
    }, []);
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const availableKeys = React.useMemo(() => {
        const availableKeys = shortcutKeys_1.shortcutKeys.filter((key) => adaptable.api.shortcutApi.getShortcuts().every((shortcut) => shortcut.ShortcutKey !== key));
        if (shortcut && StringExtensions_1.default.IsNotNullOrEmptyOrWhiteSpace(shortcut.ShortcutKey)) {
            availableKeys.push(shortcut.ShortcutKey);
        }
        availableKeys.sort();
        return availableKeys;
    }, []);
    const dispatch = (0, react_redux_1.useDispatch)();
    const handleFinish = () => {
        if (props.data) {
            dispatch(ShortcutRedux.ShortcutEdit(shortcut));
        }
        else {
            dispatch(ShortcutRedux.ShortcutAdd(shortcut));
        }
        props.onFinishWizard(shortcut);
    };
    return (React.createElement(OnePageAdaptableWizard_1.OnePageAdaptableWizard, { defaultCurrentSectionName: props.defaultCurrentSectionName, moduleInfo: props.moduleInfo, data: shortcut, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
            {
                title: 'Settings',
                isValid: ShortcutSettingsWizard_1.isSettingsValid,
                details: (React.createElement(React.Fragment, null,
                    "A Shortcut is a mathematical calculation (",
                    React.createElement("b", null, "Operation"),
                    ") which is applied with the defined operand ",
                    React.createElement("b", null, "Value"),
                    " on the edited cell content when the corresponding Shortcut ",
                    React.createElement("b", null, "Key"),
                    " is pressed.")),
                renderSummary: () => React.createElement(ShortcutSettingsWizard_1.ShortcutSettingsSummary, null),
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(ShortcutSettingsWizard_1.ShortcutSettingsWizard, { availableKeys: props.availableKeys ?? availableKeys, onChange: setShortcut }))),
            },
            {
                title: 'Target',
                isValid: NewScopeComponent_1.isScopeValid,
                details: 'Specify where Shortcut should be applied',
                renderSummary: () => (0, NewScopeComponent_1.renderScopeSummary)(shortcut.Scope, {
                    scopeWholeRow: 'Shortcut is triggered for all numeric cells',
                    scopeColumns: 'Shortcut is triggered for cells in selected columns',
                    scopeDataTypes: 'Shortcut is triggered for all numeric cells',
                }),
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(ShortcutScopeWizardSection_1.ShortcutScopeWizardSection, { onChange: setShortcut }))),
            },
            {
                details: 'Select Shortcut Tags',
                title: 'Tags',
                isVisible: (_, api) => api.internalApi.shouldDisplayTagSections(),
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(ObjectTagsWizardSection_1.ObjectTagsWizardSection, { onChange: setShortcut }))),
                renderSummary: ObjectTagsWizardSection_1.renderObjectTagsSummary,
            },
            '-',
            {
                details: 'Review the Shortcut',
                render: () => {
                    return (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                        React.createElement(OnePageAdaptableWizard_1.OnePageWizardSummary, null)));
                },
                title: 'Summary',
            },
        ] }));
};
exports.ShortcutWizard = ShortcutWizard;
