import { Fragment, jsx } from "react/jsx-runtime";
import react, { useContext, useEffect, useRef, useState } from "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { createStyles } from "antd-style";
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      overflow: hidden;\n      position: relative;\n      .tb-card-item {\n        height: 100%;\n        .ant-card {\n          display: flex;\n          flex-direction: column;\n          height: 100%;\n          .ant-card-body {\n            height: 1px;\n            flex: 1;\n            display: flex;\n            flex-direction: column;\n            overflow: hidden;\n          }\n        }\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const FixedBlockContext = /*#__PURE__*/ react.createContext({
    setFixedBlock: ()=>{},
    height: 0,
    fixedBlockUID: false,
    fixedBlockUIDRef: {
        current: false
    },
    inFixedBlock: false
});
const useFixedSchema = ()=>{
    var _field_decoratorProps;
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { setFixedBlock, fixedBlockUID, fixedBlockUIDRef } = useFixedBlock();
    const hasSet = useRef(false);
    useEffect(()=>{
        if (!fixedBlockUIDRef.current || hasSet.current) {
            var _field_decoratorProps;
            setFixedBlock((null == field ? void 0 : null == (_field_decoratorProps = field.decoratorProps) ? void 0 : _field_decoratorProps.fixedBlock) ? fieldSchema['x-uid'] : false);
            hasSet.current = true;
        }
    }, [
        null == field ? void 0 : null == (_field_decoratorProps = field.decoratorProps) ? void 0 : _field_decoratorProps.fixedBlock
    ]);
    return fieldSchema['x-uid'] === fixedBlockUID;
};
const useFixedBlock = ()=>useContext(FixedBlockContext);
const FixedBlockWrapper = (props)=>{
    const fixedBlock = useFixedSchema();
    const { height, fixedBlockUID } = useFixedBlock();
    if (!fixedBlock && fixedBlockUID) return /*#__PURE__*/ jsx(Fragment, {
        children: props.children
    });
    return /*#__PURE__*/ jsx("div", {
        className: "tb-fixed-block",
        style: {
            height: fixedBlockUID ? "calc(100vh - ".concat(height, ")") : void 0
        },
        children: props.children
    });
};
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        fixedBlockCss: css(_templateObject())
    };
});
const FixedBlock = (props)=>{
    const { height } = props;
    const { styles } = useStyles();
    const [fixedBlockUID, _setFixedBlock] = useState(false);
    const fixedBlockUIDRef = useRef(fixedBlockUID);
    const setFixedBlock = (v)=>{
        fixedBlockUIDRef.current = v;
        _setFixedBlock(v);
    };
    return /*#__PURE__*/ jsx(FixedBlockContext.Provider, {
        value: {
            inFixedBlock: true,
            height,
            setFixedBlock,
            fixedBlockUID,
            fixedBlockUIDRef
        },
        children: /*#__PURE__*/ jsx("div", {
            className: fixedBlockUID ? styles.fixedBlockCss : '',
            style: {
                height: fixedBlockUID ? "calc(100vh - ".concat(height, ")") : void 0
            },
            children: props.children
        })
    });
};
const page_FixedBlock = FixedBlock;
export { FixedBlockWrapper, page_FixedBlock as default, useFixedBlock, useFixedSchema };
