/**
 * amis v3.5.1
 * build time: 2023-11-02
 * Copyright 2018-2023 baidu
 */

import { __rest, __assign } from 'tslib';
import { createObject, padArr } from 'amis-core';
import { Icon } from 'amis-ui';
import { observer } from 'mobx-react';
import React from 'react';

function AutoFilterForm(_a) {
    var autoGenerateFilter = _a.autoGenerateFilter, searchFormExpanded = _a.searchFormExpanded, activedSearchableColumns = _a.activedSearchableColumns, searchableColumns = _a.searchableColumns, onItemToggleExpanded = _a.onItemToggleExpanded, onToggleExpanded = _a.onToggleExpanded, cx = _a.classnames, __ = _a.translate, render = _a.render, data = _a.data, onSearchableFromReset = _a.onSearchableFromReset, onSearchableFromSubmit = _a.onSearchableFromSubmit, onSearchableFromInit = _a.onSearchableFromInit, popOverContainer = _a.popOverContainer;
    var schema = React.useMemo(function () {
        var _a = typeof autoGenerateFilter === 'boolean'
            ? {
                columnsNum: 3,
                showBtnToolbar: true
            }
            : autoGenerateFilter, columnsNum = _a.columnsNum, showBtnToolbar = _a.showBtnToolbar;
        var body = padArr(activedSearchableColumns, columnsNum).map(function (group) { return ({
            type: 'group',
            body: group.map(function (column) {
                var _a, _b, _c, _d;
                return (__assign(__assign({}, (column.searchable === true
                    ? {
                        type: 'input-text',
                        name: column.name,
                        label: column.label
                    }
                    : __assign({ type: 'input-text', name: column.name }, column.searchable))), { name: (_b = (_a = column.searchable) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : column.name, label: (_d = (_c = column.searchable) === null || _c === void 0 ? void 0 : _c.label) !== null && _d !== void 0 ? _d : column.label }));
            })
        }); });
        var showExpander = searchableColumns.length >= columnsNum;
        // todo 以后做动画
        if (!searchFormExpanded && body.length) {
            body.splice(1, body.length - 1);
            body[0].body.splice(columnsNum - 1, body[0].body.length - columnsNum + 1);
        }
        var lastGroup = body[body.length - 1];
        if (!Array.isArray(lastGroup === null || lastGroup === void 0 ? void 0 : lastGroup.body) ||
            lastGroup.body.length >= columnsNum) {
            lastGroup = {
                type: 'group',
                body: []
            };
            body.push(lastGroup);
        }
        var count = Math.max(columnsNum - lastGroup.body.length - 1);
        while (count-- > 0) {
            lastGroup.body.push({
                type: 'tpl',
                tpl: ''
            });
        }
        lastGroup.body.push({
            type: 'container',
            className: 'ButtonToolbar text-right block',
            wrapperBody: false,
            body: [
                {
                    type: 'dropdown-button',
                    label: __('Table.searchFields'),
                    className: cx('Table-searchableForm-dropdown', 'mr-2'),
                    level: 'link',
                    trigger: 'click',
                    size: 'sm',
                    align: 'right',
                    visible: showBtnToolbar,
                    buttons: searchableColumns.map(function (column) {
                        var _a, _b, _c, _d;
                        return {
                            type: 'checkbox',
                            label: false,
                            className: cx('Table-searchableForm-checkbox'),
                            inputClassName: cx('Table-searchableForm-checkbox-inner'),
                            name: "".concat(column.searchable.strategy === 'jsonql' ? '' : '__search_').concat((_b = (_a = column.searchable) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : column.name),
                            option: (_d = (_c = column.searchable) === null || _c === void 0 ? void 0 : _c.label) !== null && _d !== void 0 ? _d : column.label,
                            /**
                             * syncLocation开启后，参数值会从地址栏Query中二次同步到数据域中，其中布尔(boolean)类型的值被转化为字符串
                             * eg:
                             *     true ==> "true"
                             *     false ==> "false"
                             * 所以这里将真值和假值转化成字符串格式规避
                             */
                            trueValue: '1',
                            falseValue: '0',
                            value: !!column.enableSearch ? '1' : '0',
                            badge: {
                                offset: [-10, 5],
                                visibleOn: "".concat(column.toggable && !column.toggled && column.enableSearch)
                            },
                            onChange: function (value) {
                                return onItemToggleExpanded === null || onItemToggleExpanded === void 0 ? void 0 : onItemToggleExpanded(column, value === '1' ? true : false);
                            }
                        };
                    })
                },
                {
                    type: 'submit',
                    label: __('search'),
                    level: 'primary',
                    className: 'w-18'
                },
                {
                    type: 'reset',
                    label: __('reset'),
                    className: 'w-18'
                },
                showExpander
                    ? {
                        children: function () { return (React.createElement("a", { className: cx('Table-SFToggler', searchFormExpanded ? 'is-expanded' : ''), onClick: onToggleExpanded },
                            __(searchFormExpanded ? 'collapse' : 'expand'),
                            React.createElement("span", { className: cx('Table-SFToggler-arrow') },
                                React.createElement(Icon, { icon: "right-arrow-bold", className: "icon" })))); }
                    }
                    : null
            ].filter(function (item) { return item; })
        });
        return {
            type: 'form',
            api: null,
            title: '',
            mode: 'horizontal',
            submitText: __('search'),
            body: body,
            actions: [],
            canAccessSuperData: false
        };
    }, [
        autoGenerateFilter,
        activedSearchableColumns,
        searchableColumns,
        searchFormExpanded
    ]);
    return render('searchable-form', schema, {
        key: 'searchable-form',
        panelClassName: cx('Table-searchableForm'),
        actionsClassName: cx('Table-searchableForm-footer'),
        onReset: onSearchableFromReset,
        onSubmit: onSearchableFromSubmit,
        onInit: onSearchableFromInit,
        formStore: undefined,
        data: data,
        popOverContainer: popOverContainer
    });
}
var AutoFilterForm$1 = observer(function (_a) {
    var store = _a.store, query = _a.query, data = _a.data, rest = __rest(_a, ["store", "query", "data"]);
    var onItemToggleExpanded = React.useCallback(function (column, value) {
        column.setEnableSearch(value);
        store.setSearchFormExpanded(true);
    }, []);
    var onToggleExpanded = React.useCallback(function () {
        store.toggleSearchFormExpanded();
    }, []);
    var ctx = React.useMemo(function () { return (query ? createObject(data, query) : data); }, [query, data]);
    return (React.createElement(AutoFilterForm, __assign({}, rest, { activedSearchableColumns: store.activedSearchableColumns, searchableColumns: store.searchableColumns, searchFormExpanded: store.searchFormExpanded, onItemToggleExpanded: onItemToggleExpanded, onToggleExpanded: onToggleExpanded, data: ctx })));
});

export { AutoFilterForm, AutoFilterForm$1 as default };
window.amisVersionInfo={version:'3.5.1',buildTime:'2023-11-02'};
