"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    ReadPrettyInternalViewer: ()=>ReadPrettyInternalViewer,
    isObject: ()=>isObject
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_react_i18next_namespaceObject = require("react-i18next");
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 DeclareVariable_js_namespaceObject = require("../../../modules/variable/DeclareVariable.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_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;
};
function isObject(value) {
    return 'object' == typeof value && null !== value;
}
const ReadPrettyInternalViewer = (0, schema_namespaceObject.observer)((props)=>{
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const recordCtx = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const { getCollection } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const { enableLink } = fieldSchema['x-component-props'] || {};
    const field = (0, schema_namespaceObject.useField)();
    const defFieldNames = (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 { snapshot } = (0, external_action_index_js_namespaceObject.useActionContext)();
    const targetCollection = getCollection(null == collectionField ? void 0 : collectionField.target);
    const isTreeCollection = (null == targetCollection ? void 0 : targetCollection.template) === 'tree';
    const ellipsisWithTooltipRef = (0, external_react_namespaceObject.useRef)();
    const getLabelUiSchema = (0, external_util_js_namespaceObject.useLabelUiSchemaV2)();
    const { formulaRecord, fieldNames } = customTitle(recordCtx, fieldSchema, defFieldNames);
    const renderRecords = ()=>(0, schema_namespaceObject.toArr)(formulaRecord[fieldSchema['name']] || 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, external_InternalCascadeSelect_js_namespaceObject.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, external_util_js_namespaceObject.getLabelFormatValue)(compile(labelUiSchema), val, true);
            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) || void 0 === _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.id, "_").concat(index));
        });
    const renderWithoutTableFieldResourceProvider = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DeclareVariable_js_namespaceObject.DeclareVariable, {
            name: "$nPopupRecord",
            title: t('Current popup record'),
            value: record,
            collection: targetCollection,
            children: /*#__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 ? 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: formulaRecord[collectionFieldNames[1]],
            children: renderWithoutTableFieldResourceProvider()
        }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
            record: record,
            parent: formulaRecord,
            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) && 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: 'drawer',
                            snapshot: (null == collectionField ? void 0 : collectionField.interface) === 'snapshot',
                            fieldSchema: fieldSchema
                        },
                        children: renderRecordProvider()
                    })
                ]
            })
        })
    });
}, {
    displayName: 'ReadPrettyInternalViewer'
});
const customTitle = (record, schema, fieldNames)=>{
    var _schema_xcomponentprops, _schema_xcomponentprops_fieldNames, _schema_xcomponentprops1;
    if ((null === (_schema_xcomponentprops = schema['x-component-props']) || void 0 === _schema_xcomponentprops ? void 0 : _schema_xcomponentprops.mode) === 'CustomTitle' && (null === (_schema_xcomponentprops1 = schema['x-component-props']) || void 0 === _schema_xcomponentprops1 ? void 0 : null === (_schema_xcomponentprops_fieldNames = _schema_xcomponentprops1.fieldNames) || void 0 === _schema_xcomponentprops_fieldNames ? void 0 : _schema_xcomponentprops_fieldNames.formula)) {
        var _schema_xcomponentprops_fieldNames1, _schema_xcomponentprops2;
        const regex = /{{(.*?)}}/g;
        const valueOject = {};
        let outputStr = '';
        let match;
        const formula = null === (_schema_xcomponentprops2 = schema['x-component-props']) || void 0 === _schema_xcomponentprops2 ? void 0 : null === (_schema_xcomponentprops_fieldNames1 = _schema_xcomponentprops2.fieldNames) || void 0 === _schema_xcomponentprops_fieldNames1 ? void 0 : _schema_xcomponentprops_fieldNames1.formula;
        while(match = regex.exec(formula))if (match[1].includes('.')) {
            const fieldList = [
                schema.name
            ];
            fieldList.push(...match[1].split('.'));
            const result = {
                currRecord: record,
                value: ''
            };
            fieldList.forEach((value, index)=>{
                var _result_currRecord;
                if (fieldList.length - 1 !== index) {
                    var _result_currRecord1;
                    result.currRecord = null === (_result_currRecord1 = result.currRecord) || void 0 === _result_currRecord1 ? void 0 : _result_currRecord1[value];
                    return;
                }
                result.value = null === (_result_currRecord = result.currRecord) || void 0 === _result_currRecord ? void 0 : _result_currRecord[value];
            });
            var _result_value;
            valueOject[match[1]] = null !== (_result_value = result.value) && void 0 !== _result_value ? _result_value : '';
        } else {
            var _record_schema_name;
            valueOject[match[1]] = (null === (_record_schema_name = record[schema['name']]) || void 0 === _record_schema_name ? void 0 : _record_schema_name[match[1]]) || '';
        }
        outputStr = replacePlaceholders(formula, valueOject);
        record[schema['name']] = {
            ...record[schema['name']],
            customLabel: outputStr
        };
        return {
            formulaRecord: record,
            fieldNames: {
                ...fieldNames,
                label: 'customLabel'
            }
        };
    }
    return {
        formulaRecord: record,
        fieldNames
    };
};
const replacePlaceholders = (inputStr, values)=>inputStr.replace(/{{(.*?)}}/g, function(match, placeholder) {
        return Object.prototype.hasOwnProperty.call(values, placeholder) ? values[placeholder] : match;
    });
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
});
