"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const useLogic = ({ aside, header, formProps, }) => {
    const [headerActions, setHeaderActions] = (0, react_1.useState)(undefined);
    const containerRef = (0, react_1.useRef)(null);
    const getHeaderProps = () => {
        if (!(0, react_1.isValidElement)(header)) {
            // biome-ignore lint/nursery/noShadow: <>
            const { actions, ...restHeaderProps } = header;
            return { actions, restHeaderProps };
        }
        return { actions: undefined, restHeaderProps: undefined };
    };
    const { actions, restHeaderProps } = getHeaderProps();
    const { isSubmitting } = formProps.form?.formState || {};
    const isDisabled = formProps.isDisabled ||
        Boolean([...(actions?.main || []), ...(formProps.secondaryActions || [])].find((action) => action.isBlockingOperation && action.loading));
    const updateActions = (0, react_1.useCallback)((items) => items?.map((item) => ({
        ...item,
        disabled: isSubmitting || isDisabled ? true : item.disabled,
    })), [isSubmitting, isDisabled]);
    (0, react_1.useEffect)(() => {
        if (!actions) {
            setHeaderActions(undefined);
            return;
        }
        setHeaderActions({
            main: updateActions(actions?.main),
            secondary: updateActions(actions?.secondary),
            secondaryVisible: updateActions(actions?.secondaryVisible),
        });
    }, [actions, isSubmitting, isDisabled]);
    return {
        headerProps: {
            actions: headerActions,
            ...restHeaderProps,
        },
        pageContentProps: {
            isSeparatorShown: Boolean(aside),
        },
        renderChildrenProps: {
            isDisabled: isDisabled || isSubmitting,
            containerRef,
        },
        headerActions,
    };
};
exports.useLogic = useLogic;
