"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BulkUpdateValueSelector = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const FieldWrap_1 = tslib_1.__importDefault(require("../../../components/FieldWrap"));
const UIHelper_1 = tslib_1.__importDefault(require("../../UIHelper"));
const AdaptableInput_1 = tslib_1.__importDefault(require("../AdaptableInput"));
const rebass_1 = require("rebass");
const PermittedValuesSelector_1 = require("./PermittedValuesSelector");
const Select_1 = require("../../../components/Select");
const AdaptableContext_1 = require("../../AdaptableContext");
const react_1 = require("react");
const BulkUpdateValueSelector = (props) => {
    const [valueType, setValueType] = React.useState('existing');
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const columnDataType = props.selectedColumn?.dataType;
    const isDateType = props.selectedColumn && columnDataType === 'date';
    const columnId = props.selectedColumn?.columnId;
    // TODO = this seems to be called whenever we edit a cell
    // which is then triggering a get all values
    // not sure that we need to
    const permittedValueSelector = (React.createElement(rebass_1.Box, null,
        React.createElement(PermittedValuesSelector_1.PermittedValuesSelector, { allowNewValues: true, searchable: 'inline', disabled: props.disabled || !props.selectedColumn, value: props.selectedColumnValue === '' ? null : props.selectedColumnValue, columnId: columnId, placeholder: isDateType ? 'Select' : 'Select or type new value', loadValues: (0, react_1.useCallback)(({ currentSearchValue }) => {
                if (!columnId || !props.selectedGridCells.length) {
                    return Promise.resolve([]);
                }
                return adaptable.api.gridApi.internalApi.getDistinctEditDisplayValuesForColumn({
                    columnId,
                    gridCell: props.selectedGridCells[0],
                    currentSearchValue,
                });
            }, [
                columnId,
                props.selectedGridCells?.[0]?.column.columnId,
                props.selectedGridCells?.[0]?.primaryKeyValue,
            ]), onChange: (value) => {
                props.onColumnValueChange(value || null);
            } })));
    const input = (React.createElement(AdaptableInput_1.default, { "data-name": "bulkupdate-value-input", "aria-label": "Bulk Update Value Input", type: props.selectedColumn
            ? UIHelper_1.default.getDescriptionForDataType(props.selectedColumn.dataType)
            : 'text', placeholder: props.selectedColumn
            ? UIHelper_1.default.getPlaceholderForDataType(props.selectedColumn.dataType)
            : 'Enter Value', autoFocus: true, disabled: props.disabled, style: { width: '100%' }, value: props.selectedColumnValue, onChange: (e) => {
            props.onColumnValueChange(e.target.value);
        } }));
    let valueTypeSelector = null;
    if (isDateType) {
        valueTypeSelector = (React.createElement(Select_1.Select, { "aria-label": "Bulk Update Value Selector", options: [
                { label: 'New', value: 'new' },
                { label: 'Existing', value: 'existing' },
            ], value: valueType, onChange: (value) => {
                setValueType(value);
            } }));
    }
    return (React.createElement(FieldWrap_1.default, { className: props.className, style: {
            ...props.style,
            overflow: 'visible',
            maxWidth: '100%',
        } }, isDateType ? (React.createElement(React.Fragment, null,
        valueType === 'new' ? input : permittedValueSelector,
        valueTypeSelector)) : (permittedValueSelector)));
};
exports.BulkUpdateValueSelector = BulkUpdateValueSelector;
