import * as React from 'react';
import Input from '../../../components/Input';
import FormLayout, { FormRow } from '../../../components/FormLayout';
import { Tabs } from '../../../components/Tabs';
import { useOnePageAdaptableWizardContext, } from '../../Wizard/OnePageAdaptableWizard';
import { PlusMinusRuleSummary, PlusMinusRuleWizardSection } from './PlusMinusRuleWizardSection';
import { isAdaptableRuleValid } from '../../Components/EntityRulesEditor/Utilities';
import { SummaryText } from '../../../View/Wizard/OnePageAdaptableWizard';
import { Tag } from '../../../components/Tag';
import { Flex } from '../../../components/Flex';
export const PlusMinusSettingsSummary = (props) => {
    const { data } = useOnePageAdaptableWizardContext();
    return (React.createElement(React.Fragment, null,
        React.createElement(SummaryText, null,
            "Name ",
            React.createElement(Tag, null, data.Name || 'Not specified')),
        React.createElement(SummaryText, null,
            "Nudge Value ",
            React.createElement(Tag, null, data.NudgeValue ?? 'Not selected')),
        React.createElement(SummaryText, null,
            "Nudge Type ",
            React.createElement(Tag, null, props.hasCondition ? 'Conditional' : 'Always Apply')),
        props.hasCondition && React.createElement(PlusMinusRuleSummary, null)));
};
export const isSettingsValid = (hasCondition) => (data, api, context) => {
    if (!data.Name?.trim()) {
        return '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 already exists with that name';
    }
    if (hasCondition) {
        if (!data?.Rule?.BooleanExpression) {
            return 'No valid Rule is specified';
        }
        const ruleValidation = 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;
};
export const PlusMinusSettingsWizardSection = (props) => {
    const { data } = 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, { flexDirection: "column", "data-name": "plus-minus-column-settings", className: "twa:h-full" },
        React.createElement(Tabs, { className: "twa:p-2" },
            React.createElement(Tabs.Tab, null, "Settings"),
            React.createElement(Tabs.Content, null,
                React.createElement(FormLayout, null,
                    React.createElement(FormRow, { label: "Name" },
                        React.createElement(Input, { "data-name": "plus-minus-name", className: "twa:flex-1 twa:mr-3", onChange: handleNameChange, placeholder: "Enter Name", value: data.Name ?? '' })),
                    React.createElement(FormRow, { label: "Nudge Value" },
                        React.createElement(Input, { "data-name": "nudge-value", className: "twa:flex-1 twa:mr-3", onChange: handleNudgeValueChange, placeholder: "Enter Number", type: "number", value: data.NudgeValue ?? '' }))))),
        React.createElement(Tabs, { className: "twa:p-2", selectedIndex: props.hasCondition ? 1 : 0, onSelectedIndexChange: (index) => props.onConditionChange(index === 1) },
            React.createElement(Tabs.Tab, { "data-name": "nudge-type-always" }, "Always Apply"),
            React.createElement(Tabs.Tab, { "data-name": "nudge-type-condition" }, "Conditional"),
            React.createElement(Tabs.Content, null,
                React.createElement(Flex, { className: "twa:text-2 twa:py-2" }, "The nudge value is always applied when using Plus/Minus")),
            React.createElement(Tabs.Content, { className: "twa:p-0" },
                React.createElement(PlusMinusRuleWizardSection, { defaultPredicateId: "NonBlanks", onChange: props.onChange })))));
};
