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_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_variable_DeclareVariable_mjs__ from "../../../modules/variable/DeclareVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__core_index_mjs__ from "../../core/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__action_index_mjs__ from "../action/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__input_EllipsisWithTooltip_mjs__ from "../input/EllipsisWithTooltip.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_mjs__ from "./hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalCascadeSelect_mjs__ from "./InternalCascadeSelect.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_mjs__ from "./schema.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__util_mjs__ from "./util.mjs";
const toValue = (value, placeholder)=>{
    if (null == value) return placeholder;
    return value;
};
function isObject(value) {
    return 'object' == typeof value && null !== value;
}
const ReadPrettyInternalViewer = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const recordCtx = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { enableLink } = fieldSchema['x-component-props'] || {};
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldNames = (0, __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useFieldNames)(props);
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const insertViewer = (0, __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useInsertSchema)('Viewer');
    const { options: collectionField } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useAssociationFieldContext)();
    const [record, setRecord] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const { designable } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { snapshot } = (0, __WEBPACK_EXTERNAL_MODULE__action_index_mjs__.useActionContext)();
    const targetCollection = getCollection(null == collectionField ? void 0 : collectionField.target);
    const isTreeCollection = (null == targetCollection ? void 0 : targetCollection.template) === 'tree';
    const ellipsisWithTooltipRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)();
    const getLabelUiSchema = (0, __WEBPACK_EXTERNAL_MODULE__util_mjs__.useLabelUiSchemaV2)();
    const renderRecords = ()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.toArr)(props.value).map((record, index, arr)=>{
            var _getCollection_options, _getCollection, _targetCollection_fieldsMap_, _targetCollection_fieldsMap, _getCollection_options1, _getCollection1, _targetCollection_fieldsMap_1, _targetCollection_fieldsMap1;
            const value = null == record ? void 0 : record[(null == fieldNames ? void 0 : fieldNames.label) || 'label'];
            const label = isTreeCollection ? (0, __WEBPACK_EXTERNAL_MODULE__InternalCascadeSelect_mjs__.transformNestedData)(record).map((o)=>null == o ? void 0 : o[(null == fieldNames ? void 0 : fieldNames.label) || 'label']).join(' / ') : isObject(value) ? (null === (_getCollection = getCollection(null == targetCollection ? void 0 : null === (_targetCollection_fieldsMap = targetCollection.fieldsMap) || void 0 === _targetCollection_fieldsMap ? void 0 : null === (_targetCollection_fieldsMap_ = _targetCollection_fieldsMap[(null == fieldNames ? void 0 : fieldNames.label) || 'label']) || void 0 === _targetCollection_fieldsMap_ ? void 0 : _targetCollection_fieldsMap_.target)) || void 0 === _getCollection ? void 0 : null === (_getCollection_options = _getCollection.options) || void 0 === _getCollection_options ? void 0 : _getCollection_options.titleField) ? value[null === (_getCollection1 = getCollection(null == targetCollection ? void 0 : null === (_targetCollection_fieldsMap1 = targetCollection.fieldsMap) || void 0 === _targetCollection_fieldsMap1 ? void 0 : null === (_targetCollection_fieldsMap_1 = _targetCollection_fieldsMap1[(null == fieldNames ? void 0 : fieldNames.label) || 'label']) || void 0 === _targetCollection_fieldsMap_1 ? void 0 : _targetCollection_fieldsMap_1.target)) || void 0 === _getCollection1 ? void 0 : null === (_getCollection_options1 = _getCollection1.options) || void 0 === _getCollection_options1 ? void 0 : _getCollection_options1.titleField] : JSON.stringify(value) : value;
            const val = toValue(compile(label), 'N/A');
            const labelUiSchema = getLabelUiSchema((null == record ? void 0 : record.__collection) || (null == collectionField ? void 0 : collectionField.target), (null == fieldNames ? void 0 : fieldNames.label) || 'label');
            const text = (0, __WEBPACK_EXTERNAL_MODULE__util_mjs__.getLabelFormatValue)(compile(labelUiSchema), val, true);
            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react__.Fragment, {
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                        children: snapshot ? text : false !== enableLink ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                            onClick: (e)=>{
                                var _ellipsisWithTooltipRef_current;
                                e.stopPropagation();
                                e.preventDefault();
                                if (designable) insertViewer(__WEBPACK_EXTERNAL_MODULE__schema_mjs__["default"].Viewer);
                                setVisible(true);
                                setRecord(record);
                                null == ellipsisWithTooltipRef || null === (_ellipsisWithTooltipRef_current = ellipsisWithTooltipRef.current) || void 0 === _ellipsisWithTooltipRef_current || _ellipsisWithTooltipRef_current.setPopoverVisible(false);
                            },
                            children: text
                        }) : text
                    }),
                    index < arr.length - 1 ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                        style: {
                            marginRight: 4,
                            color: '#aaa'
                        },
                        children: ","
                    }) : null
                ]
            }, "".concat(null == record ? void 0 : record.id, "_").concat(index));
        });
    const renderWithoutTableFieldResourceProvider = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__modules_variable_DeclareVariable_mjs__.DeclareVariable, {
            name: "$nPopupRecord",
            title: t('Current popup record'),
            value: record,
            collection: targetCollection,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.WithoutTableFieldResource.Provider, {
                value: true,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__core_index_mjs__.FormProvider, {
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                        schema: fieldSchema,
                        onlyRenderProperties: true,
                        basePath: field.address,
                        filterProperties: (s)=>'AssociationField.Viewer' === s['x-component']
                    })
                })
            })
        });
    const renderRecordProvider = ()=>{
        var _fieldSchema_xcollectionfield;
        const collectionFieldNames = null == fieldSchema ? void 0 : null === (_fieldSchema_xcollectionfield = fieldSchema['x-collection-field']) || void 0 === _fieldSchema_xcollectionfield ? void 0 : _fieldSchema_xcollectionfield.split('.');
        return collectionFieldNames && collectionFieldNames.length > 2 ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
            record: record,
            parent: recordCtx[collectionFieldNames[1]],
            children: renderWithoutTableFieldResourceProvider()
        }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
            record: record,
            parent: recordCtx,
            children: renderWithoutTableFieldResourceProvider()
        });
    };
    var _collectionField_target;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.BlockAssociationContext.Provider, {
            value: "".concat(null == collectionField ? void 0 : collectionField.collectionName, ".").concat(null == collectionField ? void 0 : collectionField.name),
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.CollectionProvider_deprecated, {
                name: null !== (_collectionField_target = null == collectionField ? void 0 : collectionField.target) && void 0 !== _collectionField_target ? _collectionField_target : null == collectionField ? void 0 : collectionField.targetCollection,
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__input_EllipsisWithTooltip_mjs__.EllipsisWithTooltip, {
                        ellipsis: true,
                        ref: ellipsisWithTooltipRef,
                        children: renderRecords()
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__action_index_mjs__.ActionContextProvider, {
                        value: {
                            visible,
                            setVisible,
                            openMode: 'drawer',
                            snapshot: (null == collectionField ? void 0 : collectionField.interface) === 'snapshot',
                            fieldSchema: fieldSchema
                        },
                        children: renderRecordProvider()
                    })
                ]
            })
        })
    });
}, {
    displayName: 'ReadPrettyInternalViewer'
});
export { ReadPrettyInternalViewer, isObject };
