"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UploadFiles = void 0;
var react_1 = __importStar(require("react"));
var upload_files_js_1 = require("@devesharp/upload-files-js");
var useEventListener_1 = require("../../hooks/useEventListener");
var UploadFiles_style_1 = __importDefault(require("./UploadFiles.style"));
exports.UploadFiles = react_1.default.forwardRef(function UploadFiles(_a, ref) {
    var _b = _a.drag, drag = _b === void 0 ? false : _b, _c = _a.multiple, multiple = _c === void 0 ? false : _c, options = _a.options, children = _a.children;
    var inputRef = react_1.useRef();
    var uploadManager = react_1.useRef(new upload_files_js_1.UploadManager(options)).current;
    var _d = react_1.useState(false), dragging = _d[0], setDragging = _d[1];
    function dropFile(ev) {
        if (drag) {
            ev.preventDefault();
            setDragging(false);
            Array.from(ev.dataTransfer.files).forEach(function (file) { return uploadManager.addFile(file); });
        }
    }
    function onChangeFile(ev) {
        Array.from(ev.target.files).forEach(function (file) { return uploadManager.addFile(file); });
        if (inputRef.current) {
            inputRef.current.value = '';
        }
    }
    useEventListener_1.useEventListener('drop', function (ev) {
        if (!drag) {
            ev.preventDefault();
        }
    }, inputRef);
    react_1.useImperativeHandle(ref, function () { return ({
        cancelFile: function (file) {
            uploadManager.cancelFile(file);
        },
        retryFilesError: function (file) {
            uploadManager.retryFilesError();
        },
    }); }, []);
    return (react_1.default.createElement(UploadFiles_style_1.default, { onDragEnter: function () { return drag && setDragging(true); }, onDragLeave: function () { return drag && setDragging(false); }, onDrop: dropFile },
        react_1.default.createElement("input", { type: "file", ref: inputRef, onChange: onChangeFile, multiple: multiple }),
        children({ dragging: dragging })));
});
//# sourceMappingURL=UploadFiles.js.map