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__block_provider_index_mjs__ from "../../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_BlockProvider_mjs__ from "../../../block-provider/BlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.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__preview_index_mjs__ from "../preview/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InputRecordPicker_mjs__ from "./InputRecordPicker.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__useFieldNames_mjs__ from "./useFieldNames.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__util_mjs__ from "./util.mjs";
const toValue = (value, placeholder)=>{
    if (null == value) return placeholder;
    return value;
};
const ReadPrettyRecordPicker = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    var _fieldSchema_xcomponentprops;
    const { ellipsis } = props;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const recordCtx = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const fieldNames = (0, __WEBPACK_EXTERNAL_MODULE__useFieldNames_mjs__.useFieldNames)(props);
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const collectionField = getField(fieldSchema.name) || getCollectionJoinField(null == fieldSchema ? void 0 : fieldSchema['x-collection-field']);
    const [record, setRecord] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const labelUiSchema = (0, __WEBPACK_EXTERNAL_MODULE__util_mjs__.useLabelUiSchema)(collectionField, (null == fieldNames ? void 0 : fieldNames.label) || 'label');
    const showFilePicker = (0, __WEBPACK_EXTERNAL_MODULE__InputRecordPicker_mjs__.isShowFilePicker)(labelUiSchema);
    const { snapshot } = (0, __WEBPACK_EXTERNAL_MODULE__action_index_mjs__.useActionContext)();
    const isTagsMode = (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.mode) === 'tags';
    const ellipsisWithTooltipRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)();
    if (showFilePicker) return collectionField ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__preview_index_mjs__.Preview, {
        ...props
    }) : null;
    const renderRecords = ()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.toArr)(props.value).map((record, index, arr)=>{
            const val = toValue(compile(null == record ? void 0 : record[(null == fieldNames ? void 0 : fieldNames.label) || 'label']), 'N/A');
            const text = (0, __WEBPACK_EXTERNAL_MODULE__util_mjs__.getLabelFormatValue)(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", {
                        className: "test-record-picker-read-pretty-item",
                        children: snapshot || isTagsMode ? text : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                            onClick: (e)=>{
                                var _ellipsisWithTooltipRef_current;
                                e.stopPropagation();
                                e.preventDefault();
                                setVisible(true);
                                setRecord(record);
                                null == ellipsisWithTooltipRef || null === (_ellipsisWithTooltipRef_current = ellipsisWithTooltipRef.current) || void 0 === _ellipsisWithTooltipRef_current || _ellipsisWithTooltipRef_current.setPopoverVisible(false);
                            },
                            children: text
                        })
                    }),
                    index < arr.length - 1 ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                        style: {
                            marginRight: 4,
                            color: '#aaa'
                        },
                        children: ","
                    }) : null
                ]
            }, "".concat(record[fieldNames.value], "_").concat(index));
        });
    const renderWithoutTableFieldResourceProvider = ()=>/*#__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,
                    filterProperties: (s)=>'RecordPicker.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 collectionField ? /*#__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_BlockProvider_mjs__.BlockAssociationContext.Provider, {
            value: "".concat(collectionField.collectionName, ".").concat(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 = collectionField.target) && void 0 !== _collectionField_target ? _collectionField_target : collectionField.targetCollection,
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__input_EllipsisWithTooltip_mjs__.EllipsisWithTooltip, {
                        ellipsis: ellipsis,
                        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: 'snapshot' === collectionField.interface
                        },
                        children: renderRecordProvider()
                    })
                ]
            })
        })
    }) : null;
}, {
    displayName: 'ReadPrettyRecordPicker'
});
export { ReadPrettyRecordPicker };
