"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlusMinusSettingsWizardSection = exports.isSettingsValid = exports.PlusMinusSettingsSummary = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const Input_1 = tslib_1.__importDefault(require("../../../components/Input"));
const Radio_1 = tslib_1.__importDefault(require("../../../components/Radio"));
const FormLayout_1 = tslib_1.__importStar(require("../../../components/FormLayout"));
const Tabs_1 = require("../../../components/Tabs");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const PlusMinusRuleWizardSection_1 = require("./PlusMinusRuleWizardSection");
const Utilities_1 = require("../../Components/EntityRulesEditor/Utilities");
const OnePageAdaptableWizard_2 = require("../../../View/Wizard/OnePageAdaptableWizard");
const Tag_1 = require("../../../components/Tag");
const PlusMinusSettingsSummary = (props) => {
    const { data } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (React.createElement(React.Fragment, null,
        React.createElement(OnePageAdaptableWizard_2.SummaryText, null,
            "Nudge Value ",
            React.createElement(Tag_1.Tag, null, data.NudgeValue ?? 'Not selected')),
        props.hasCondition && React.createElement(PlusMinusRuleWizardSection_1.PlusMinusRuleSummary, null)));
};
exports.PlusMinusSettingsSummary = PlusMinusSettingsSummary;
const isSettingsValid = (hasCondition) => (data, api, context) => {
    if (hasCondition) {
        if (!data?.Rule?.BooleanExpression) {
            return 'No valid Rule is specified';
        }
        const ruleValidation = (0, Utilities_1.isAdaptableRuleValid)(data, api, context);
        if (typeof ruleValidation === 'string') {
            return ruleValidation;
        }
    }
    if (typeof data.NudgeValue !== 'number') {
        return 'Nudge value is not specified';
    }
    if (data.NudgeValue === 0) {
        return 'Nudge value must be different than 0';
    }
    return true;
};
exports.isSettingsValid = isSettingsValid;
const PlusMinusSettingsWizardSection = (props) => {
    const { data } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const handleNudgeValueChange = (event) => {
        const newValue = parseFloat(event.target.value);
        props.onChange({
            ...data,
            NudgeValue: isNaN(newValue) ? '' : newValue,
        });
    };
    return (React.createElement(rebass_1.Flex, { height: "100%", flexDirection: "column", "data-name": "plus-minus-column-settings" },
        React.createElement(Tabs_1.Tabs, { autoFocus: false, padding: 2 },
            React.createElement(Tabs_1.Tabs.Tab, null, "Plus Minus Settings"),
            React.createElement(Tabs_1.Tabs.Content, null,
                React.createElement(FormLayout_1.default, null,
                    React.createElement(FormLayout_1.FormRow, { label: "Nudge Value" },
                        React.createElement(Input_1.default, { "data-name": "nudge-value", flex: 1, marginRight: 3, onChange: handleNudgeValueChange, placeholder: "Enter Number", type: "number", value: data.NudgeValue ?? '' })),
                    React.createElement(FormLayout_1.FormRow, { label: "Nudge Type" },
                        React.createElement(rebass_1.Flex, null,
                            React.createElement(Radio_1.default, { "data-name": "nudge-type-always", value: "Always", checked: !props.hasCondition, onChange: () => props.onConditionChange(false) }, "Always Apply Nudge"),
                            React.createElement(Radio_1.default, { "data-name": "nudge-type-condition", marginLeft: 3, value: "Expression", checked: props.hasCondition, onChange: () => props.onConditionChange(true) }, "Create a Rule")))))),
        props.hasCondition && (React.createElement(PlusMinusRuleWizardSection_1.PlusMinusRuleWizardSection, { defaultPredicateId: "NonBlanks", onChange: props.onChange }))));
};
exports.PlusMinusSettingsWizardSection = PlusMinusSettingsWizardSection;
