import { jsx } from "react/jsx-runtime";
import "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useCollection_deprecated } from "../../../../collection-manager/hooks/useCollection_deprecated.mjs";
import { useDataBlockProps, useDataBlockRequest } from "../../../../data-source/index.mjs";
import { useDesignable } from "../../../../schema-component/index.mjs";
import { SchemaSettingsModalItem, useCollectionState } from "../../../../schema-settings/index.mjs";
const setDataLoadingModeSettingsItem = {
    name: 'setDataLoadingMode',
    Component: SetDataLoadingMode
};
function useDataLoadingMode() {
    const { dataLoadingMode } = useDataBlockProps() || {};
    return dataLoadingMode || 'auto';
}
function SetDataLoadingMode() {
    var _fieldSchema_xdecoratorprops;
    const { dn } = useDesignable();
    const { t } = useTranslation();
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { name } = useCollection_deprecated();
    const { getEnableFieldTree, getOnLoadData } = useCollectionState(name);
    const request = useDataBlockRequest();
    return /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
        title: t('Set data loading mode'),
        scope: {
            getEnableFieldTree,
            name,
            getOnLoadData
        },
        schema: {
            type: 'object',
            title: t('Data loading mode'),
            properties: {
                dataLoadingMode: {
                    'x-decorator': 'FormItem',
                    'x-component': 'Radio.Group',
                    default: (null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops.dataLoadingMode) || 'auto',
                    enum: [
                        {
                            value: 'auto',
                            label: t('Automatically load data')
                        },
                        {
                            value: 'manual',
                            label: t('Load data after filtering')
                        }
                    ]
                }
            }
        },
        onSubmit: (param)=>{
            let { dataLoadingMode } = param;
            lodash.set(fieldSchema, 'x-decorator-props.dataLoadingMode', dataLoadingMode);
            field.decoratorProps.dataLoadingMode = dataLoadingMode;
            dn.emit('patch', {
                schema: {
                    ['x-uid']: fieldSchema['x-uid'],
                    'x-decorator-props': {
                        ...fieldSchema['x-decorator-props']
                    }
                }
            });
            dn.refresh();
            if ('auto' === dataLoadingMode) request.run();
            else request.mutate(void 0);
        }
    });
}
export { SetDataLoadingMode, setDataLoadingModeSettingsItem, useDataLoadingMode };
