"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 Input_1 = tslib_1.__importDefault(require("../../../components/Input"));
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 Flex_1 = require("../../../components/Flex");
const PlusMinusSettingsSummary = (props) => {
    const { data } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (React.createElement(React.Fragment, null,
        React.createElement(OnePageAdaptableWizard_2.SummaryText, null,
            "Name ",
            React.createElement(Tag_1.Tag, null, data.Name || 'Not specified')),
        React.createElement(OnePageAdaptableWizard_2.SummaryText, null,
            "Nudge Value ",
            React.createElement(Tag_1.Tag, null, data.NudgeValue ?? 'Not selected')),
        React.createElement(OnePageAdaptableWizard_2.SummaryText, null,
            "Nudge Type ",
            React.createElement(Tag_1.Tag, null, props.hasCondition ? 'Conditional' : 'Always Apply')),
        props.hasCondition && React.createElement(PlusMinusRuleWizardSection_1.PlusMinusRuleSummary, null)));
};
exports.PlusMinusSettingsSummary = PlusMinusSettingsSummary;
const isSettingsValid = (hasCondition) => (data, api, context) => {
    if (!data.Name?.trim()) {
        return 'A name is required.';
    }
    const allPlusMinusNudges = api.plusMinusApi.getAllPlusMinus();
    const isDuplicateName = allPlusMinusNudges.some((nudge) => nudge.Name === data.Name && nudge.Uuid !== data.Uuid);
    if (isDuplicateName) {
        return 'A Plus/Minus Nudge with this name already exists.';
    }
    if (hasCondition) {
        if (!data?.Rule?.BooleanExpression) {
            return 'A valid rule is required.';
        }
        const ruleValidation = (0, Utilities_1.isAdaptableRuleValid)(data, api, context);
        if (typeof ruleValidation === 'string') {
            return ruleValidation;
        }
    }
    if (typeof data.NudgeValue !== 'number') {
        return 'A nudge value is required.';
    }
    if (data.NudgeValue === 0) {
        return 'Nudge value must not be zero.';
    }
    return true;
};
exports.isSettingsValid = isSettingsValid;
const PlusMinusSettingsWizardSection = (props) => {
    const { data } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const handleNameChange = (event) => {
        props.onChange({
            ...data,
            Name: event.target.value,
        });
    };
    const handleNudgeValueChange = (event) => {
        const newValue = parseFloat(event.target.value);
        props.onChange({
            ...data,
            NudgeValue: isNaN(newValue) ? '' : newValue,
        });
    };
    return (React.createElement(Flex_1.Flex, { flexDirection: "column", "data-name": "plus-minus-column-settings", className: "twa:h-full" },
        React.createElement(Tabs_1.Tabs, { className: "twa:p-2" },
            React.createElement(Tabs_1.Tabs.Tab, null, "Settings"),
            React.createElement(Tabs_1.Tabs.Content, null,
                React.createElement(FormLayout_1.default, null,
                    React.createElement(FormLayout_1.FormRow, { label: "Name" },
                        React.createElement(Input_1.default, { "data-name": "plus-minus-name", className: "twa:flex-1 twa:mr-3", onChange: handleNameChange, placeholder: "Enter Name", value: data.Name ?? '' })),
                    React.createElement(FormLayout_1.FormRow, { label: "Nudge Value" },
                        React.createElement(Input_1.default, { "data-name": "nudge-value", className: "twa:flex-1 twa:mr-3", onChange: handleNudgeValueChange, placeholder: "Enter Number", type: "number", value: data.NudgeValue ?? '' }))))),
        React.createElement(Tabs_1.Tabs, { className: "twa:p-2", selectedIndex: props.hasCondition ? 1 : 0, onSelectedIndexChange: (index) => props.onConditionChange(index === 1) },
            React.createElement(Tabs_1.Tabs.Tab, { "data-name": "nudge-type-always" }, "Always Apply"),
            React.createElement(Tabs_1.Tabs.Tab, { "data-name": "nudge-type-condition" }, "Conditional"),
            React.createElement(Tabs_1.Tabs.Content, null,
                React.createElement(Flex_1.Flex, { className: "twa:text-2 twa:py-2" }, "The nudge value is always applied when using Plus/Minus")),
            React.createElement(Tabs_1.Tabs.Content, { className: "twa:p-0" },
                React.createElement(PlusMinusRuleWizardSection_1.PlusMinusRuleWizardSection, { defaultPredicateId: "NonBlanks", onChange: props.onChange })))));
};
exports.PlusMinusSettingsWizardSection = PlusMinusSettingsWizardSection;
