import { children, createSignal, mergeProps, splitProps } from "solid-js";
import { Transition } from "solid-react-transition";
const defaultProps = {};
// Normalizes Transition callbacks when nodeRef is used.
const TransitionWrapper = (p) => {
    const [local, props] = splitProps(mergeProps(defaultProps, p), [
        "onEnter",
        "onEntering",
        "onEntered",
        "onExit",
        "onExiting",
        "onExited",
        "addEndListener",
        "children",
        "childRef",
    ]);
    let [nodeRef, setNodeRef] = createSignal();
    const mergedRef = (ref) => {
        setNodeRef(ref);
        local.childRef?.(ref);
    };
    function normalize(callback) {
        return (param) => {
            if (callback && nodeRef()) {
                callback(nodeRef(), param);
            }
        };
    }
    const handlers = {
        get onEnter() {
            return normalize(local.onEnter);
        },
        get onEntering() {
            return normalize(local.onEntering);
        },
        get onEntered() {
            return normalize(local.onEntered);
        },
        get onExit() {
            return normalize(local.onExit);
        },
        get onExiting() {
            return normalize(local.onExiting);
        },
        get onExited() {
            return normalize(local.onExited);
        },
        get addEndListener() {
            return normalize(local.addEndListener);
        },
    };
    function renderChild() {
        if (typeof local.children === "function") {
            // wrap function to get ref
            return (status, innerProps) => local.children?.(status, {
                ...innerProps,
                ref: mergedRef,
            });
        }
        else {
            // get resolved ref now
            let childRef = children(() => local.children)();
            mergedRef(childRef);
            return childRef;
        }
    }
    return (<Transition {...props} {...handlers} nodeRef={nodeRef()}>
      {renderChild()}
    </Transition>);
};
export default TransitionWrapper;
