"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (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 = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (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__, {
    ReadPrettyInternalTag: ()=>ReadPrettyInternalTag
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_index_js_namespaceObject = require("../../index.js");
const index_js_namespaceObject = require("../../../block-provider/index.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_hooks_js_namespaceObject = require("./hooks.js");
const external_InternalCascadeSelect_js_namespaceObject = require("./InternalCascadeSelect.js");
const external_InternalViewer_js_namespaceObject = require("./InternalViewer.js");
const external_schema_js_namespaceObject = require("./schema.js");
var external_schema_js_default = /*#__PURE__*/ __webpack_require__.n(external_schema_js_namespaceObject);
const external_util_js_namespaceObject = require("./util.js");
const toValue = (value, placeholder)=>{
    if (null == value) return placeholder;
    return value;
};
const ReadPrettyInternalTag = (0, schema_namespaceObject.observer)((props)=>{
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const recordCtx = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const { enableLink, tagColorField } = fieldSchema['x-component-props'];
    const field = (0, schema_namespaceObject.useField)();
    const fieldNames = (0, external_hooks_js_namespaceObject.useFieldNames)(props);
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const insertViewer = (0, external_hooks_js_namespaceObject.useInsertSchema)('Viewer');
    const { options: collectionField } = (0, external_hooks_js_namespaceObject.useAssociationFieldContext)();
    const [record, setRecord] = (0, external_react_namespaceObject.useState)({});
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const { designable } = (0, external_index_js_namespaceObject.useDesignable)();
    const labelUiSchema = (0, external_util_js_namespaceObject.useLabelUiSchema)(collectionField, (null == fieldNames ? void 0 : fieldNames.label) || 'label');
    const { snapshot } = (0, external_action_index_js_namespaceObject.useActionContext)();
    const ellipsisWithTooltipRef = (0, external_react_namespaceObject.useRef)();
    const { getCollection } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const targetCollection = getCollection(null == collectionField ? void 0 : collectionField.target);
    const isTreeCollection = (null == targetCollection ? void 0 : targetCollection.template) === 'tree';
    const renderRecords = ()=>(0, schema_namespaceObject.toArr)(props.value).map((record, index, arr)=>{
            const value = null == record ? void 0 : record[(null == fieldNames ? void 0 : fieldNames.label) || 'label'];
            const label = isTreeCollection ? (0, external_InternalCascadeSelect_js_namespaceObject.transformNestedData)(record).map((o)=>null == o ? void 0 : o[(null == fieldNames ? void 0 : fieldNames.label) || 'label']).join(' / ') : (0, external_InternalViewer_js_namespaceObject.isObject)(value) ? JSON.stringify(value) : value;
            const val = toValue(compile(label), 'N/A');
            const text = (0, external_util_js_namespaceObject.getTabFormatValue)(compile(labelUiSchema), val, record[tagColorField]);
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_react_namespaceObject.Fragment, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                        children: snapshot ? text : false !== enableLink ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("a", {
                            onClick: (e)=>{
                                var _ellipsisWithTooltipRef_current;
                                e.stopPropagation();
                                e.preventDefault();
                                if (designable) insertViewer(external_schema_js_default().Viewer);
                                setVisible(true);
                                setRecord(record);
                                null == ellipsisWithTooltipRef || null == (_ellipsisWithTooltipRef_current = ellipsisWithTooltipRef.current) || _ellipsisWithTooltipRef_current.setPopoverVisible(false);
                            },
                            children: text
                        }) : text
                    }),
                    index < arr.length - 1 ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                        style: {
                            marginRight: 4,
                            color: '#aaa'
                        },
                        children: ","
                    }) : null
                ]
            }, "".concat(null == record ? void 0 : 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,
                    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.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 /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.BlockAssociationContext.Provider, {
            value: "".concat(null == collectionField ? void 0 : collectionField.collectionName, ".").concat(null == collectionField ? void 0 : collectionField.name),
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_collection_manager_index_js_namespaceObject.CollectionProvider_deprecated, {
                name: null != (_collectionField_target = null == collectionField ? void 0 : collectionField.target) ? _collectionField_target : null == collectionField ? void 0 : collectionField.targetCollection,
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EllipsisWithTooltip_js_namespaceObject.EllipsisWithTooltip, {
                        ellipsis: true,
                        ref: ellipsisWithTooltipRef,
                        children: renderRecords()
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_action_index_js_namespaceObject.ActionContextProvider, {
                        value: {
                            visible,
                            setVisible,
                            openMode: external_action_index_js_namespaceObject.OpenMode.DEFAULT,
                            snapshot: (null == collectionField ? void 0 : collectionField.interface) === 'snapshot'
                        },
                        children: renderRecordProvider()
                    })
                ]
            })
        })
    });
}, {
    displayName: 'ReadPrettyInternalTag'
});
exports.ReadPrettyInternalTag = __webpack_exports__.ReadPrettyInternalTag;
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
    "ReadPrettyInternalTag"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
    value: true
});
