"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const shared_ui_1 = require("@rpldy/shared-ui");
const react_1 = require("react");
const asUploadButton = (Component, inputProps) => {
    const AsUploadButton = (props, ref) => {
        const { showFileUpload, getInternalFileInput } = (0, shared_ui_1.useUploadyContext)();
        const { id, className, text, children, extraProps, onClick } = props, uploadOptions = tslib_1.__rest(props, ["id", "className", "text", "children", "extraProps", "onClick"]);
        //using ref so onButtonClick can stay memoized
        const uploadOptionsRef = (0, react_1.useRef)();
        uploadOptionsRef.current = uploadOptions;
        const onButtonClick = (0, react_1.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 (0, jsx_runtime_1.jsx)(Component, Object.assign({ ref: ref, onClick: onButtonClick, id: id, className: className, children: children || text || 'Upload' }, extraProps));
    };
    (0, shared_ui_1.markAsUploadOptionsComponent)(AsUploadButton);
    return (0, react_1.forwardRef)(AsUploadButton);
};
exports.default = asUploadButton;
