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_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__popover_index_mjs__ from "../popover/index.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      padding-top: 0;\n      .ant-popover-arrow {\n        display: none;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        overlay: css(_templateObject())
    };
});
const Editable = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const containerRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { styles } = useStyles();
    const value = field.value;
    const schema = {
        name: fieldSchema.name,
        'x-collection-field': fieldSchema['x-collection-field'],
        'x-component': 'CollectionField',
        'x-read-pretty': true,
        default: value,
        'x-component-props': fieldSchema['x-component-props']
    };
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)({
            values: {
                [fieldSchema.name]: value
            }
        }), [
        field.value,
        fieldSchema['x-component-props']
    ]);
    const getContainer = ()=>containerRef.current;
    const modifiedChildren = __WEBPACK_EXTERNAL_MODULE_react__["default"].Children.map(props.children, (child)=>{
        if (/*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].isValidElement(child)) return /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].cloneElement(child, {
            getContainer
        });
        return child;
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormItem, {
        ...props,
        labelStyle: {
            display: 'none'
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__popover_index_mjs__.StablePopover, {
            content: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                style: {
                    width: '100%',
                    height: '100%',
                    minWidth: 500
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    ref: containerRef,
                    children: modifiedChildren
                })
            }),
            trigger: "click",
            placement: 'bottomLeft',
            overlayClassName: styles.overlay,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                style: {
                    minHeight: 30,
                    padding: '0 8px'
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FormContext.Provider, {
                    value: form,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                        schema: schema,
                        name: fieldSchema.name
                    })
                })
            })
        })
    });
}, {
    displayName: 'Editable'
});
const QuickEdit = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const collectionField = getCollectionJoinField(fieldSchema['x-collection-field']);
    if (!collectionField) return null;
    return field.editable ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Editable, {
        ...props
    }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormItem, {
        ...props,
        style: {
            padding: '0 8px'
        }
    });
}, {
    displayName: 'QuickEdit'
});
export { Editable, QuickEdit };
