import { jsx, jsxs } from "react/jsx-runtime";
import { useMemo, useState } from "react";
import { ArrayTable } from "@tachybase/components";
import { uid } from "@tachybase/schema";
import cloneDeep from "lodash/cloneDeep";
import set from "lodash/set";
import { useTranslation } from "react-i18next";
import { useAPIClient, useRequest } from "../../api-client/index.mjs";
import { useCollectionParentRecordData } from "../../data-source/index.mjs";
import { RecordProvider, useRecord } from "../../record-provider/index.mjs";
import { ActionContextProvider, SchemaComponent, useCompile } from "../../schema-component/index.mjs";
import { useCollectionManager_deprecated } from "../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs_99c14d58__ from "./components/index.mjs";
const getSchema = (schema, record, compile, getContainer)=>{
    var _record___parent;
    if (!schema) return;
    const properties = cloneDeep(schema.properties);
    properties.name['x-disabled'] = true;
    if (true === schema.hasDefaultValue) {
        properties['defaultValue'] = cloneDeep(schema.default.uiSchema);
        properties['defaultValue']['title'] = compile('{{ t("Default value") }}');
        properties['defaultValue']['x-decorator'] = 'FormItem';
    }
    return {
        type: 'object',
        properties: {
            [uid()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-component-props': {
                    getContainer: '{{ getContainer }}'
                },
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return useRequest(()=>Promise.resolve({
                                data: cloneDeep(schema.default)
                            }), options);
                    }
                },
                title: "".concat(compile(null == (_record___parent = record.__parent) ? void 0 : _record___parent.title), " - ").concat(compile('{{ t("View") }}')),
                properties: {
                    summary: {
                        type: 'void',
                        'x-component': 'FieldSummary',
                        'x-component-props': {
                            schemaKey: schema.name
                        }
                    },
                    ...properties,
                    description: {
                        type: 'string',
                        title: '{{t("Description")}}',
                        'x-decorator': 'FormItem',
                        'x-component': 'Input.TextArea'
                    }
                }
            }
        }
    };
};
const ViewCollectionField = (props)=>{
    const record = useRecord();
    const parentRecordData = useCollectionParentRecordData();
    return /*#__PURE__*/ jsx(ViewFieldAction, {
        item: record,
        parentItem: parentRecordData,
        ...props
    });
};
const ViewFieldAction = (props)=>{
    const { scope, getContainer, item: record, parentItem: parentRecord, children } = props;
    const { getInterface, collections } = useCollectionManager_deprecated();
    const [visible, setVisible] = useState(false);
    const [schema, setSchema] = useState({});
    const api = useAPIClient();
    const { t } = useTranslation();
    const compile = useCompile();
    const [data, setData] = useState({});
    const currentCollections = useMemo(()=>collections.map((v)=>({
                label: compile(v.title),
                value: v.name
            })), []);
    return /*#__PURE__*/ jsx(RecordProvider, {
        record: record,
        parent: parentRecord,
        children: /*#__PURE__*/ jsxs(ActionContextProvider, {
            value: {
                visible,
                setVisible
            },
            children: [
                /*#__PURE__*/ jsx("a", {
                    onClick: async ()=>{
                        const { data } = await api.resource('collections.fields', record.collectionName).get({
                            filterByTk: record.name,
                            appends: [
                                'reverseField'
                            ]
                        });
                        setData(null == data ? void 0 : data.data);
                        const interfaceConf = getInterface(record.interface);
                        const defaultValues = cloneDeep(null == data ? void 0 : data.data) || {};
                        if (!(null == defaultValues ? void 0 : defaultValues.reverseField)) {
                            var _interfaceConf_default;
                            defaultValues.autoCreateReverseField = false;
                            defaultValues.reverseField = null == (_interfaceConf_default = interfaceConf.default) ? void 0 : _interfaceConf_default.reverseField;
                            set(defaultValues.reverseField, 'name', "f_".concat(uid()));
                            set(defaultValues.reverseField, 'uiSchema.title', record.__parent.title);
                        }
                        const schema = getSchema({
                            ...interfaceConf,
                            default: defaultValues
                        }, record, compile, getContainer);
                        setSchema(schema);
                        setVisible(true);
                    },
                    children: children || t('View')
                }),
                /*#__PURE__*/ jsx(SchemaComponent, {
                    schema: schema,
                    components: {
                        ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs_99c14d58__,
                        ArrayTable: ArrayTable
                    },
                    scope: {
                        getContainer,
                        showReverseFieldConfig: !(null == data ? void 0 : data.reverseField),
                        createOnly: false,
                        collections: currentCollections,
                        ...scope
                    }
                })
            ]
        })
    });
};
export { ViewCollectionField, ViewFieldAction };
