import * as React from 'react';
import { forwardRef, useImperativeHandle } from 'react';
import { renderWithAdaptableContext } from '../View/renderWithAdaptableContext';
import { AdaptableFloatingFilter } from '../View/Components/ColumnFilter/AdaptableFloatingFilter';
import { getAgGridFilterNotifyModelFn } from './getAgGridFilterNotifyModelFn';
const filterContainerStyle = {
    overflow: 'hidden',
    minWidth: '0',
    height: '100%',
    display: 'flex',
    alignItems: 'stretch',
    position: 'relative',
    flex: '1 1 auto',
};
export const FloatingFilterWrapperFactory = (adaptable) => {
    const adaptableApi = adaptable.api;
    function getContainerId(colId) {
        return `floatingFilter_${colId}_${adaptable.adaptableOptions.adaptableId}`;
    }
    function getFilterProps(colId) {
        const column = adaptableApi.columnApi.getColumnWithColumnId(colId);
        const columnFilterProps = {
            Column: column,
            Adaptable: adaptable,
            ShowCloseButton: false,
        };
        return columnFilterProps;
    }
    function getNotifyModel(colId, onModelChange) {
        return getAgGridFilterNotifyModelFn(adaptableApi, colId, onModelChange);
    }
    function patchParentElement(filterContainer) {
        if (filterContainer) {
            // todo: consider theme
            filterContainer.parentElement.style.overflow = 'visible';
            filterContainer.parentElement.parentElement.style.padding = 'var(--ab-space-1)';
        }
    }
    if (adaptable.isAgGridInitialising) {
        if (adaptable.variant === 'react') {
            return () => React.createElement(React.Fragment, null);
        }
        return class FloatingFilterStub {
            init(params) {
                this.filterContainer = document.createElement('div');
            }
            getGui() {
                return this.filterContainer;
            }
            destroy() {
                this.unmountReactRoot?.();
                this.filterContainer = null;
            }
            onParentModelChanged() {
                // nothing yet
            }
        };
    }
    if (adaptable.variant === 'react') {
        return forwardRef(function ReactFloatingFilterWrapper(props, ref) {
            const colId = props.column.getId();
            const { onModelChange } = props;
            const column = adaptableApi.columnApi.getColumnWithColumnId(colId);
            useImperativeHandle(ref, () => {
                return {
                    onParentModelChanged() { },
                };
            });
            const notifyModel = getNotifyModel(colId, onModelChange);
            if (!column) {
                return null;
            }
            return renderWithAdaptableContext(React.createElement("div", { ref: patchParentElement, id: getContainerId(colId), style: filterContainerStyle },
                React.createElement(AdaptableFloatingFilter, { ...getFilterProps(colId), onChange: notifyModel, onClear: notifyModel })), adaptable);
        });
    }
    return class FloatingFilterWrapper {
        onParentModelChanged(parentModel, filterChangedEvent) {
            // todo?
        }
        afterGuiAttached() {
            patchParentElement(this.filterContainer);
        }
        init(params) {
            const colId = params.column.getColId();
            this.filterContainer = document.createElement('div');
            this.filterContainer.id = getContainerId(colId);
            Object.keys(filterContainerStyle).forEach((key) => {
                //@ts-ignore
                this.filterContainer.style[key] = filterContainerStyle[key];
            });
            const notifyModel = getNotifyModel(colId, params.filterParams.filterChangedCallback);
            const column = adaptableApi.columnApi.getColumnWithColumnId(colId);
            if (column) {
                const filterProps = getFilterProps(colId);
                this.unmountReactRoot = adaptable.renderReactRoot(renderWithAdaptableContext(React.createElement(AdaptableFloatingFilter, {
                    ...filterProps,
                    onChange: notifyModel,
                    onClear: notifyModel,
                }), adaptable), this.filterContainer);
            }
        }
        getGui() {
            return this.filterContainer;
        }
        destroy() {
            this.unmountReactRoot?.();
            this.filterContainer = null;
        }
    };
};
