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_set__ from "lodash/set";
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__data_source_index_mjs__ from "../../data-source/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__hooks_index_mjs__ from "../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "./components/index.mjs";
const getSchema = (schema, record, compile, getContainer)=>{
    var _record___parent;
    if (!schema) return;
    const properties = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(schema.properties);
    properties.name['x-disabled'] = true;
    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-component-props': {
                    getContainer: '{{ getContainer }}'
                },
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(()=>Promise.resolve({
                                data: (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(schema.default)
                            }), options);
                    }
                },
                title: "".concat(compile(null === (_record___parent = record.__parent) || void 0 === _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 = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const parentRecordData = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionParentRecordData)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ViewFieldAction, {
        item: record,
        parentItem: parentRecordData,
        ...props
    });
};
const ViewFieldAction = (props)=>{
    const { scope, getContainer, item: record, parentItem: parentRecord, children } = props;
    const { getInterface, collections } = (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 api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const [data, setData] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const currentCollections = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>collections.map((v)=>({
                label: compile(v.title),
                value: v.name
            })), []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
        record: record,
        parent: parentRecord,
        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", {
                    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 = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(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 ? void 0 : _interfaceConf_default.reverseField;
                            (0, __WEBPACK_EXTERNAL_MODULE_lodash_set__["default"])(defaultValues.reverseField, 'name', "f_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()));
                            (0, __WEBPACK_EXTERNAL_MODULE_lodash_set__["default"])(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__*/ (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,
                        showReverseFieldConfig: !(null == data ? void 0 : data.reverseField),
                        createOnly: false,
                        collections: currentCollections,
                        ...scope
                    }
                })
            ]
        })
    });
};
export { ViewCollectionField, ViewFieldAction };
