import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useMemo } from "react";
import { FormLayout } from "@tachybase/components";
import { FormContext, createForm, useField } from "@tachybase/schema";
import { createStyles } from "antd-style";
import lodash from "lodash";
import { withDynamicSchemaProps } from "../../../application/hoc/withDynamicSchemaProps.mjs";
import { BlockProvider, useBlockRequestContext, useParsedFilter } from "../../../block-provider/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      .ant-description-input {\n        line-height: 34px;\n      }\n      .ant-formily-item-feedback-layout-loose {\n        margin-bottom: 12px;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        container: css(_templateObject())
    };
});
const ListBlockContext = /*#__PURE__*/ createContext({});
ListBlockContext.displayName = 'ListBlockContext';
const InternalListBlockProvider = (props)=>{
    var _service_data;
    const { resource, service } = useBlockRequestContext();
    const { styles } = useStyles();
    const field = useField();
    const form = useMemo(()=>createForm({
            readPretty: true
        }), []);
    useEffect(()=>{
        if (!(null == service ? void 0 : service.loading)) {
            var _service_data;
            form.setValuesIn(field.address.concat('list').toString(), null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.data);
        }
    }, [
        field.address,
        form,
        null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.data,
        null == service ? void 0 : service.loading
    ]);
    return /*#__PURE__*/ jsx(ListBlockContext.Provider, {
        value: {
            service,
            resource
        },
        children: /*#__PURE__*/ jsx(FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ jsx(FormLayout, {
                layout: 'vertical',
                children: /*#__PURE__*/ jsx("div", {
                    className: styles.container,
                    children: props.children
                })
            })
        })
    });
};
const ListBlockProvider = withDynamicSchemaProps((props)=>{
    const { params } = props;
    const { filter: parsedFilter } = useParsedFilter({
        filterOption: null == params ? void 0 : params.filter
    });
    const paramsWithFilter = useMemo(()=>({
            ...params,
            filter: parsedFilter
        }), [
        parsedFilter,
        params
    ]);
    if (lodash.isEmpty(parsedFilter) && !lodash.isEmpty(null == params ? void 0 : params.filter)) return null;
    return /*#__PURE__*/ jsx(BlockProvider, {
        name: "list",
        ...props,
        params: paramsWithFilter,
        children: /*#__PURE__*/ jsx(InternalListBlockProvider, {
            ...props
        })
    });
});
const useListBlockContext = ()=>useContext(ListBlockContext);
const useListItemProps = ()=>({});
export { ListBlockContext, ListBlockProvider, useListBlockContext, useListItemProps };
