import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__ from "lodash/cloneDeep";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_omit__ from "lodash/omit";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__ from "../action-hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__ from "../ResourceActionProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "./components/index.mjs";
const getSchema = (schema, record, compile, getContainer)=>{
    if (!schema) return;
    const properties = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(schema.configurableProperties) || {
        title: {
            'x-component': 'CollectionField',
            'x-decorator': 'FormItem'
        },
        name: {
            'x-component': 'CollectionField',
            'x-decorator': 'FormItem'
        }
    };
    if (true === schema.hasDefaultValue) {
        properties['defaultValue'] = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(schema.default.uiSchema);
        properties['defaultValue']['title'] = compile('{{ t("Default value") }}');
        properties['defaultValue']['x-decorator'] = 'FormItem';
    }
    return {
        type: 'object',
        properties: {
            [(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues: '{{ useValuesFromRecord }}'
                },
                title: '{{ t("Edit collection") }}',
                properties: {
                    ...properties,
                    footer: {
                        type: 'void',
                        'x-component': 'Action.Drawer.Footer',
                        properties: {
                            action1: {
                                title: '{{ t("Cancel") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    useAction: '{{ useCancelAction }}'
                                }
                            },
                            action2: {
                                title: '{{ t("Submit") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    type: 'primary',
                                    useAction: '{{ useUpdateCollectionActionAndRefreshCM }}'
                                }
                            }
                        }
                    }
                }
            }
        }
    };
};
const useValuesFromRecord = (options)=>{
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const result = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(()=>Promise.resolve({
            data: {
                ...(0, __WEBPACK_EXTERNAL_MODULE_lodash_omit__["default"])((0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(record), [
                    '__parent',
                    '__collectionName'
                ]),
                category: null == record ? void 0 : record.category.map((v)=>v.id)
            }
        }), {
        ...options,
        manual: true
    });
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (ctx.visible) result.run();
    }, [
        ctx.visible
    ]);
    return result;
};
const useUpdateCollectionActionAndRefreshCM = (options)=>{
    const { refreshCM } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    const { refresh } = (0, __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__.useResourceActionContext)();
    const { resource, targetKey } = (0, __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__.useResourceContext)();
    const { [targetKey]: filterByTk, template } = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const collectionResource = 'sql' === template ? api.resource('sqlCollection') : resource;
    return {
        async run () {
            await form.submit();
            await collectionResource.update({
                filterByTk,
                values: 'sql' === template ? form.values : (0, __WEBPACK_EXTERNAL_MODULE_lodash_omit__["default"])(form.values, [
                    'fields'
                ])
            });
            ctx.setVisible(false);
            await form.reset();
            refresh();
            await refreshCM();
        }
    };
};
const EditCollection = (props)=>{
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(EditCollectionAction, {
        item: record,
        ...props
    });
};
const EditCollectionAction = (props)=>{
    const { scope, getContainer, item: record, children, ...otherProps } = props;
    const { getTemplate } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [schema, setSchema] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
        record: record,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.ActionContextProvider, {
            value: {
                visible,
                setVisible
            },
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                    ...otherProps,
                    onClick: async ()=>{
                        const templateConf = getTemplate(record.template);
                        const schema = getSchema({
                            ...templateConf
                        }, record, compile, getContainer);
                        setSchema(schema);
                        setVisible(true);
                    },
                    children: children || t('Edit')
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
                    schema: schema,
                    components: {
                        ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs__,
                        ArrayTable: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayTable
                    },
                    scope: {
                        getContainer,
                        useValuesFromRecord,
                        useUpdateCollectionActionAndRefreshCM,
                        useCancelAction: __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__.useCancelAction,
                        createOnly: false,
                        ...scope
                    }
                })
            ]
        })
    });
};
export { EditCollection, EditCollectionAction, useUpdateCollectionActionAndRefreshCM, useValuesFromRecord };
