"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PredicateEditor = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const Icon_1 = require("../../../components/Icon");
const HelpBlock_1 = tslib_1.__importDefault(require("../../../components/HelpBlock"));
const Tag_1 = require("../../../components/Tag");
const AdaptableInput_1 = tslib_1.__importDefault(require("../AdaptableInput"));
const PermittedValuesSelector_1 = require("../PermittedValuesSelector");
const AdaptableIconComponent_1 = require("../AdaptableIconComponent");
const Select_1 = require("../../../components/Select");
const isValuesPredicateDef = (colDef) => colDef && ['Values', 'ExcludeValues'].includes(colDef.id);
const PredicateEditor = (props) => {
    var _a, _b;
    const baseClassName = 'ab-PredicateEditor';
    const predicateDefsOptions = React.useMemo(() => {
        var _a;
        return (_a = props === null || props === void 0 ? void 0 : props.predicateDefs) === null || _a === void 0 ? void 0 : _a.map((item) => ({
            value: item.id,
            label: item.label,
            inputs: item.inputs,
        }));
    }, [props.predicateDefs, props.onChange]);
    const handlePredicateInputChange = (e, index) => {
        var _a;
        const { value } = e.target;
        const newInputs = [...((_a = props.predicate.Inputs) !== null && _a !== void 0 ? _a : [])];
        newInputs[index] = value;
        props.onChange(Object.assign(Object.assign({}, props.predicate), { Inputs: newInputs }));
    };
    const handlePredicateValuesChange = (inputs) => {
        props.onChange(Object.assign(Object.assign({}, props.predicate), { Inputs: inputs }));
    };
    const currentPredicateDef = (_a = props === null || props === void 0 ? void 0 : props.predicateDefs) === null || _a === void 0 ? void 0 : _a.find((item) => { var _a; return item.id === ((_a = props.predicate) === null || _a === void 0 ? void 0 : _a.PredicateId); });
    let icon = null;
    if (currentPredicateDef) {
        if ((0, Icon_1.isAdaptableSystemIcon)(currentPredicateDef === null || currentPredicateDef === void 0 ? void 0 : currentPredicateDef.icon)) {
            icon = React.createElement(AdaptableIconComponent_1.AdaptableIconComponent, { icon: currentPredicateDef.icon });
        }
        else if ((currentPredicateDef === null || currentPredicateDef === void 0 ? void 0 : currentPredicateDef.icon) && 'text' in (currentPredicateDef === null || currentPredicateDef === void 0 ? void 0 : currentPredicateDef.icon)) {
            icon = React.createElement("span", null, currentPredicateDef.icon.text);
        }
    }
    return (React.createElement(rebass_1.Box, { className: baseClassName },
        React.createElement(rebass_1.Flex, { justifyContent: "stretch", alignItems: "center" },
            icon && React.createElement(Tag_1.Tag, { mr: 2 }, icon),
            React.createElement(Select_1.Select, { isClearable: true, options: predicateDefsOptions, "data-name": "select-predicate", placeholder: "Select Rule", value: currentPredicateDef === null || currentPredicateDef === void 0 ? void 0 : currentPredicateDef.id, onChange: (value) => {
                    var _a;
                    const option = predicateDefsOptions.find((x) => x.value === value);
                    props.onChange(Object.assign(Object.assign({}, props.predicate), { PredicateId: option === null || option === void 0 ? void 0 : option.value, Inputs: ((_a = option === null || option === void 0 ? void 0 : option.inputs) !== null && _a !== void 0 ? _a : []).map((input) => { var _a; return (_a = input.defaultValue) !== null && _a !== void 0 ? _a : ''; }) }));
                } })),
        React.createElement(rebass_1.Flex, null, (_b = currentPredicateDef === null || currentPredicateDef === void 0 ? void 0 : currentPredicateDef.inputs) === null || _b === void 0 ? void 0 : _b.map((predicateDefInput, index) => {
            var _a, _b;
            return (React.createElement(React.Fragment, null,
                index > 0 && React.createElement(HelpBlock_1.default, { margin: 2 }, "AND"),
                React.createElement(rebass_1.Flex, { key: index, flex: 1, flexDirection: "column" },
                    React.createElement(AdaptableInput_1.default, { "data-name": `predicate-input-${index}`, marginTop: 2, type: predicateDefInput.type, autoFocus: index === 0, value: (_b = (_a = props.predicate.Inputs) === null || _a === void 0 ? void 0 : _a[index]) !== null && _b !== void 0 ? _b : '', onChange: (e) => handlePredicateInputChange(e, index) }))));
        })),
        isValuesPredicateDef(currentPredicateDef) && (React.createElement(PermittedValuesSelector_1.PermitedValuesSelector, { onPredicateValuesChange: handlePredicateValuesChange, predicate: props.predicate, columnId: props.columnId }))));
};
exports.PredicateEditor = PredicateEditor;
