import { __rest } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { markAsUploadOptionsComponent, useUploadyContext } from '@rpldy/shared-ui';
import { forwardRef, useCallback, useRef } from 'react';
const asUploadButton = (Component, inputProps) => {
    const AsUploadButton = (props, ref) => {
        const { showFileUpload, getInternalFileInput } = useUploadyContext();
        const { id, className, text, children, extraProps, onClick } = props, uploadOptions = __rest(props, ["id", "className", "text", "children", "extraProps", "onClick"]);
        //using ref so onButtonClick can stay memoized
        const uploadOptionsRef = useRef();
        uploadOptionsRef.current = uploadOptions;
        const onButtonClick = useCallback((e) => {
            const input = getInternalFileInput();
            input.current.accept = inputProps.accept;
            showFileUpload(uploadOptionsRef.current);
            onClick === null || onClick === void 0 ? void 0 : onClick(e);
        }, [getInternalFileInput, showFileUpload, uploadOptionsRef, onClick]);
        return _jsx(Component, Object.assign({ ref: ref, onClick: onButtonClick, id: id, className: className, children: children || text || 'Upload' }, extraProps));
    };
    markAsUploadOptionsComponent(AsUploadButton);
    return forwardRef(AsUploadButton);
};
export default asUploadButton;
