import { jsx, jsxs } from "react/jsx-runtime";
import { useLayoutEffect } from "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { SortableItem, useCollection_deprecated, useCompile, useDesignable, useDesigner } from "../../../index.mjs";
import { useStyles } from "./Table.Column.ActionBar.mjs";
const useColumnSchema = ()=>{
    const { getField } = useCollection_deprecated();
    const compile = useCompile();
    const columnSchema = useFieldSchema();
    const fieldSchema = columnSchema.reduceProperties((buf, s)=>{
        if ('CollectionField' === s['x-component']) return s;
        return buf;
    }, null);
    if (!fieldSchema) return {};
    const collectionField = getField(fieldSchema.name);
    return {
        columnSchema,
        fieldSchema,
        collectionField,
        uiSchema: compile(null == collectionField ? void 0 : collectionField.uiSchema)
    };
};
const TableColumnDecorator = (props)=>{
    const Designer = useDesigner();
    const field = useField();
    const { fieldSchema, uiSchema, collectionField } = useColumnSchema();
    const { styles } = useStyles();
    const { refresh } = useDesignable();
    const compile = useCompile();
    useLayoutEffect(()=>{
        if (field.title) return;
        if (!fieldSchema) return;
        if (null == uiSchema ? void 0 : uiSchema.title) field.title = null == uiSchema ? void 0 : uiSchema.title;
    }, [
        null == uiSchema ? void 0 : uiSchema.title
    ]);
    return /*#__PURE__*/ jsxs(SortableItem, {
        className: styles.designer,
        children: [
            /*#__PURE__*/ jsx(Designer, {
                fieldSchema: fieldSchema,
                uiSchema: uiSchema,
                collectionField: collectionField
            }),
            field.title || compile(null == uiSchema ? void 0 : uiSchema.title)
        ]
    });
};
export { TableColumnDecorator, useColumnSchema };
