"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgGridFilterAdapterFactory = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const AdaptableColumnFilter_1 = require("../View/Components/ColumnFilter/AdaptableColumnFilter");
const renderWithAdaptableContext_1 = require("../View/renderWithAdaptableContext");
const AgGridFilterAdapterFactory = (adaptable) => {
    function isFilterActive(colId) {
        // we need this here
        if (adaptable.isDestroyed) {
            return false;
        }
        //make the small filter icon to appear when there is a filter
        return adaptable.api.filterApi.columnFilterApi.isColumnFilterActiveForColumn(colId);
    }
    function getContainerId(colId) {
        return 'filter_' + colId + '_' + adaptable.adaptableOptions.adaptableId;
    }
    return class AgGridFilterAdapter {
        init(params) {
            this.params = params;
            this.column = params.column;
            this.filterContainer = document.createElement('div');
            this.filterContainer.id = getContainerId(this.params.column.getColId());
        }
        getGui() {
            return this.filterContainer;
        }
        refresh(newParams) {
            // always reuse the current instance
            // the filter is refreshed in the underlying React component
            return true;
        }
        afterGuiAttached(params) {
            //we always unmount first so the autofocus from the form works... in other grids we unmount when hidden
            this.unmountReactRoot?.();
            const columnId = this.column.getColId();
            let column = adaptable.api.columnApi.getColumnWithColumnId(columnId);
            if (column) {
                this.unmountReactRoot = adaptable.renderReactRoot((0, renderWithAdaptableContext_1.renderWithAdaptableContext)(React.createElement(AdaptableColumnFilter_1.AdaptableColumnFilter, {
                    columnId,
                    wrapperProps: { className: 'twa:p-2' },
                    location: 'columnMenu',
                }), adaptable), this.filterContainer);
            }
        }
        destroy() {
            this.unmountReactRoot?.();
            this.filterContainer = null;
        }
    };
};
exports.AgGridFilterAdapterFactory = AgGridFilterAdapterFactory;
