"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    ReadPrettyRecordPicker: ()=>ReadPrettyRecordPicker
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const index_js_namespaceObject = require("../../../block-provider/index.js");
const BlockProvider_js_namespaceObject = require("../../../block-provider/BlockProvider.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_record_provider_index_js_namespaceObject = require("../../../record-provider/index.js");
const external_core_index_js_namespaceObject = require("../../core/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_action_index_js_namespaceObject = require("../action/index.js");
const EllipsisWithTooltip_js_namespaceObject = require("../input/EllipsisWithTooltip.js");
const external_preview_index_js_namespaceObject = require("../preview/index.js");
const external_InputRecordPicker_js_namespaceObject = require("./InputRecordPicker.js");
const external_useFieldNames_js_namespaceObject = require("./useFieldNames.js");
const external_util_js_namespaceObject = require("./util.js");
const toValue = (value, placeholder)=>{
    if (null == value) return placeholder;
    return value;
};
const ReadPrettyRecordPicker = (0, schema_namespaceObject.observer)((props)=>{
    var _fieldSchema_xcomponentprops;
    const { ellipsis } = props;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const recordCtx = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const { getCollectionJoinField } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const fieldNames = (0, external_useFieldNames_js_namespaceObject.useFieldNames)(props);
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const { getField } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const collectionField = getField(fieldSchema.name) || getCollectionJoinField(null == fieldSchema ? void 0 : fieldSchema['x-collection-field']);
    const [record, setRecord] = (0, external_react_namespaceObject.useState)({});
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const labelUiSchema = (0, external_util_js_namespaceObject.useLabelUiSchema)(collectionField, (null == fieldNames ? void 0 : fieldNames.label) || 'label');
    const showFilePicker = (0, external_InputRecordPicker_js_namespaceObject.isShowFilePicker)(labelUiSchema);
    const { snapshot } = (0, external_action_index_js_namespaceObject.useActionContext)();
    const isTagsMode = (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.mode) === 'tags';
    const ellipsisWithTooltipRef = (0, external_react_namespaceObject.useRef)();
    if (showFilePicker) return collectionField ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_preview_index_js_namespaceObject.Preview, {
        ...props
    }) : null;
    const renderRecords = ()=>(0, schema_namespaceObject.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, external_util_js_namespaceObject.getLabelFormatValue)(labelUiSchema, val, true);
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_react_namespaceObject.Fragment, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                        className: "test-record-picker-read-pretty-item",
                        children: snapshot || isTagsMode ? text : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("span", {
                        style: {
                            marginRight: 4,
                            color: '#aaa'
                        },
                        children: ","
                    }) : null
                ]
            }, "".concat(record[fieldNames.value], "_").concat(index));
        });
    const renderWithoutTableFieldResourceProvider = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.WithoutTableFieldResource.Provider, {
            value: true,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_core_index_js_namespaceObject.FormProvider, {
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
            record: record,
            parent: recordCtx[collectionFieldNames[1]],
            children: renderWithoutTableFieldResourceProvider()
        }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
            record: record,
            parent: recordCtx,
            children: renderWithoutTableFieldResourceProvider()
        });
    };
    var _collectionField_target;
    return collectionField ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BlockProvider_js_namespaceObject.BlockAssociationContext.Provider, {
            value: "".concat(collectionField.collectionName, ".").concat(collectionField.name),
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_collection_manager_index_js_namespaceObject.CollectionProvider_deprecated, {
                name: null !== (_collectionField_target = collectionField.target) && void 0 !== _collectionField_target ? _collectionField_target : collectionField.targetCollection,
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EllipsisWithTooltip_js_namespaceObject.EllipsisWithTooltip, {
                        ellipsis: ellipsis,
                        ref: ellipsisWithTooltipRef,
                        children: renderRecords()
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_action_index_js_namespaceObject.ActionContextProvider, {
                        value: {
                            visible,
                            setVisible,
                            openMode: 'drawer',
                            snapshot: 'snapshot' === collectionField.interface
                        },
                        children: renderRecordProvider()
                    })
                ]
            })
        })
    }) : null;
}, {
    displayName: 'ReadPrettyRecordPicker'
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
