import { jsx, jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { ArrayItems } from "@tachybase/components";
import { useField, useFieldSchema } from "@tachybase/schema";
import { Slider } from "antd";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useFormBlockContext } from "../../../block-provider/index.mjs";
import { useCollection_deprecated, useSortFields } from "../../../collection-manager/index.mjs";
import { SetDataLoadingMode } from "../../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem.mjs";
import { useRecord } from "../../../record-provider/index.mjs";
import { GeneralSchemaDesigner, SchemaSettingsDivider, SchemaSettingsModalItem, SchemaSettingsRemove, SchemaSettingsSelectItem, SchemaSettingsTemplate } from "../../../schema-settings/index.mjs";
import { SchemaSettingsDataScope } from "../../../schema-settings/SchemaSettingsDataScope.mjs";
import { useSchemaTemplate } from "../../../schema-templates/index.mjs";
import { SchemaComponentOptions } from "../../core/index.mjs";
import { useDesignable } from "../../hooks/index.mjs";
import { removeNullCondition } from "../filter/index.mjs";
import { defaultColumnCount, gridSizes, pageSizeOptions, screenSizeMaps, screenSizeTitleMaps } from "./options.mjs";
const columnCountMarks = [
    1,
    2,
    3,
    4,
    6,
    8,
    12,
    24
].reduce((obj, cur)=>{
    obj[cur] = cur;
    return obj;
}, {});
const GridCardDesigner = ()=>{
    var _fieldSchema_xdecoratorprops_params, _fieldSchema_xdecoratorprops, _fieldSchema_xdecoratorprops1, _fieldSchema_xdecoratorprops2, _fieldSchema_xdecoratorprops_params1, _fieldSchema_xdecoratorprops3, _field_decoratorProps_params, _field_decoratorProps;
    const { name, title } = useCollection_deprecated();
    const template = useSchemaTemplate();
    const { t } = useTranslation();
    const fieldSchema = useFieldSchema();
    const { form } = useFormBlockContext();
    const field = useField();
    const { dn } = useDesignable();
    const sortFields = useSortFields(name);
    useRecord();
    const defaultSort = (null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : null == (_fieldSchema_xdecoratorprops_params = _fieldSchema_xdecoratorprops.params) ? void 0 : _fieldSchema_xdecoratorprops_params.sort) || [];
    const defaultResource = (null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops1 = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops1.resource) || (null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops2 = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops2.association);
    const columnCount = field.decoratorProps.columnCount || defaultColumnCount;
    const columnCountSchema = useMemo(()=>({
            'x-component': 'Slider',
            'x-decorator': 'FormItem',
            'x-component-props': {
                min: 1,
                max: 24,
                marks: columnCountMarks,
                tooltip: {
                    formatter: (value)=>"".concat(value).concat(t('Column'))
                },
                step: null
            }
        }), [
        t
    ]);
    const columnCountProperties = useMemo(()=>gridSizes.reduce((o, k)=>{
            o[k] = {
                ...columnCountSchema,
                title: t(screenSizeTitleMaps[k]),
                description: "".concat(t('Screen size'), " ").concat(screenSizeMaps[k], " ").concat(t('pixels'))
            };
            return o;
        }, {}), [
        columnCountSchema,
        t
    ]);
    const sort = null == defaultSort ? void 0 : defaultSort.map((item)=>item.startsWith('-') ? {
            field: item.substring(1),
            direction: 'desc'
        } : {
            field: item,
            direction: 'asc'
        });
    return /*#__PURE__*/ jsx(GeneralSchemaDesigner, {
        template: template,
        title: title || name,
        children: /*#__PURE__*/ jsxs(SchemaComponentOptions, {
            components: {
                Slider: Slider
            },
            children: [
                /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
                    title: t('Set the count of columns displayed in a row'),
                    initialValues: columnCount,
                    schema: {
                        type: 'object',
                        title: t('Set the count of columns displayed in a row'),
                        properties: columnCountProperties
                    },
                    onSubmit: (columnCount)=>{
                        lodash.set(fieldSchema, 'x-decorator-props.columnCount', columnCount);
                        field.decoratorProps.columnCount = columnCount;
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-decorator-props': fieldSchema['x-decorator-props']
                            }
                        });
                    }
                }),
                /*#__PURE__*/ jsx(SchemaSettingsDataScope, {
                    collectionName: name,
                    defaultFilter: (null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops3 = fieldSchema['x-decorator-props']) ? void 0 : null == (_fieldSchema_xdecoratorprops_params1 = _fieldSchema_xdecoratorprops3.params) ? void 0 : _fieldSchema_xdecoratorprops_params1.filter) || {},
                    form: form,
                    onSubmit: (param)=>{
                        let { filter } = param;
                        filter = removeNullCondition(filter);
                        lodash.set(fieldSchema, 'x-decorator-props.params.filter', filter);
                        field.decoratorProps.params = {
                            ...fieldSchema['x-decorator-props'].params
                        };
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-decorator-props': fieldSchema['x-decorator-props']
                            }
                        });
                    }
                }),
                /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
                    title: t('Set default sorting rules'),
                    components: {
                        ArrayItems: ArrayItems
                    },
                    schema: {
                        type: 'object',
                        title: t('Set default sorting rules'),
                        properties: {
                            sort: {
                                type: 'array',
                                default: sort,
                                'x-component': 'ArrayItems',
                                'x-decorator': 'FormItem',
                                items: {
                                    type: 'object',
                                    properties: {
                                        space: {
                                            type: 'void',
                                            'x-component': 'Space',
                                            properties: {
                                                sort: {
                                                    type: 'void',
                                                    'x-decorator': 'FormItem',
                                                    'x-component': 'ArrayItems.SortHandle'
                                                },
                                                field: {
                                                    type: 'string',
                                                    enum: sortFields,
                                                    required: true,
                                                    'x-decorator': 'FormItem',
                                                    'x-component': 'Select',
                                                    'x-component-props': {
                                                        style: {
                                                            width: 260
                                                        },
                                                        optionLabelProp: 'fullLabel'
                                                    }
                                                },
                                                direction: {
                                                    type: 'string',
                                                    'x-decorator': 'FormItem',
                                                    'x-component': 'Radio.Group',
                                                    'x-component-props': {
                                                        optionType: 'button'
                                                    },
                                                    enum: [
                                                        {
                                                            label: t('ASC'),
                                                            value: 'asc'
                                                        },
                                                        {
                                                            label: t('DESC'),
                                                            value: 'desc'
                                                        }
                                                    ]
                                                },
                                                remove: {
                                                    type: 'void',
                                                    'x-decorator': 'FormItem',
                                                    'x-component': 'ArrayItems.Remove'
                                                }
                                            }
                                        }
                                    }
                                },
                                properties: {
                                    add: {
                                        type: 'void',
                                        title: t('Add sort field'),
                                        'x-component': 'ArrayItems.Addition'
                                    }
                                }
                            }
                        }
                    },
                    onSubmit: (param)=>{
                        let { sort } = param;
                        const sortArr = sort.map((item)=>'desc' === item.direction ? "-".concat(item.field) : item.field);
                        lodash.set(fieldSchema, 'x-decorator-props.params.sort', sortArr);
                        field.decoratorProps.params = {
                            ...fieldSchema['x-decorator-props'].params
                        };
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-decorator-props': fieldSchema['x-decorator-props']
                            }
                        });
                    }
                }),
                /*#__PURE__*/ jsx(SetDataLoadingMode, {}),
                /*#__PURE__*/ jsx(SchemaSettingsSelectItem, {
                    title: t('Records per page'),
                    value: (null == (_field_decoratorProps = field.decoratorProps) ? void 0 : null == (_field_decoratorProps_params = _field_decoratorProps.params) ? void 0 : _field_decoratorProps_params.pageSize) || 20,
                    options: pageSizeOptions.map((v)=>({
                            value: v
                        })),
                    onChange: (pageSize)=>{
                        lodash.set(fieldSchema, 'x-decorator-props.params.pageSize', pageSize);
                        field.decoratorProps.params = {
                            ...fieldSchema['x-decorator-props'].params,
                            page: 1
                        };
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-decorator-props': fieldSchema['x-decorator-props']
                            }
                        });
                    }
                }),
                /*#__PURE__*/ jsx(SchemaSettingsTemplate, {
                    componentName: 'GridCard',
                    collectionName: name,
                    resourceName: defaultResource
                }),
                /*#__PURE__*/ jsx(SchemaSettingsDivider, {}),
                /*#__PURE__*/ jsx(SchemaSettingsRemove, {
                    removeParentsIfNoChildren: true,
                    breakRemoveOn: {
                        'x-component': 'Grid'
                    }
                })
            ]
        })
    });
};
export { GridCardDesigner };
