import { jsx } from "react/jsx-runtime";
import react, { useMemo, useRef } from "react";
import { FormItem } from "@tachybase/components";
import { FormContext, RecursionField, createForm, observer, useField, useFieldSchema } from "@tachybase/schema";
import { createStyles } from "antd-style";
import { useCollectionManager_deprecated } from "../../../collection-manager/index.mjs";
import { StablePopover } 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 = createStyles((param)=>{
    let { css } = param;
    return {
        overlay: css(_templateObject())
    };
});
const Editable = observer((props)=>{
    const field = useField();
    const containerRef = useRef(null);
    const fieldSchema = 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 = useMemo(()=>createForm({
            values: {
                [fieldSchema.name]: value
            }
        }), [
        field.value,
        fieldSchema['x-component-props']
    ]);
    const getContainer = ()=>containerRef.current;
    const modifiedChildren = react.Children.map(props.children, (child)=>{
        if (/*#__PURE__*/ react.isValidElement(child)) return /*#__PURE__*/ react.cloneElement(child, {
            getContainer
        });
        return child;
    });
    return /*#__PURE__*/ jsx(FormItem, {
        ...props,
        labelStyle: {
            display: 'none'
        },
        children: /*#__PURE__*/ jsx(StablePopover, {
            content: /*#__PURE__*/ jsx("div", {
                style: {
                    width: '100%',
                    height: '100%',
                    minWidth: 500
                },
                children: /*#__PURE__*/ jsx("div", {
                    ref: containerRef,
                    children: modifiedChildren
                })
            }),
            trigger: "click",
            placement: 'bottomLeft',
            overlayClassName: styles.overlay,
            children: /*#__PURE__*/ jsx("div", {
                style: {
                    minHeight: 30,
                    padding: '0 8px'
                },
                children: /*#__PURE__*/ jsx(FormContext.Provider, {
                    value: form,
                    children: /*#__PURE__*/ jsx(RecursionField, {
                        schema: schema,
                        name: fieldSchema.name
                    })
                })
            })
        })
    });
}, {
    displayName: 'Editable'
});
const QuickEdit = observer((props)=>{
    const field = useField();
    const { getCollectionJoinField } = useCollectionManager_deprecated();
    const fieldSchema = useFieldSchema();
    const collectionField = getCollectionJoinField(fieldSchema['x-collection-field']);
    if (!collectionField) return null;
    return field.editable ? /*#__PURE__*/ jsx(Editable, {
        ...props
    }) : /*#__PURE__*/ jsx(FormItem, {
        ...props,
        style: {
            padding: '0 8px'
        }
    });
}, {
    displayName: 'QuickEdit'
});
export { Editable, QuickEdit };
