"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DragAndDrop = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const core_1 = require("../core");
const icon_1 = __importDefault(require("../icon"));
const t_1 = __importDefault(require("../t"));
const utils_1 = require("../utils");
const defaultIcon = 'falCloudUpload';
const defaultText = 'Click or drag files to this area';
const inputType = 'file';
const DragAndDropBase = core_1.styled.labelBox `
  display: flex;
  min-width: 0;
  transition-duration: normal;
  justify-content: center;
`;
/** Prepares uploading files by selecting or dragging. */
exports.DragAndDrop = (0, core_1.vui)((props, ref) => {
    const _a = (0, core_1.omitThemingProps)(props), { accept, className, icon = defaultIcon, selectFolderOnClick, selectFolderOnDrop, onFilesAdded, multiple = true, text = defaultText, children, disabled } = _a, rest = __rest(_a, ["accept", "className", "icon", "selectFolderOnClick", "selectFolderOnDrop", "onFilesAdded", "multiple", "text", "children", "disabled"]);
    const fileInput = (0, react_1.useRef)(null);
    const styles = (0, core_1.useStyleConfig)('DragAndDrop', props);
    const processFiles = (files) => Array.from(files).map(file => file);
    const handleFileInput = (e) => {
        var _a;
        e.preventDefault();
        if (disabled)
            return undefined;
        const files = processFiles((_a = e === null || e === void 0 ? void 0 : e.currentTarget) === null || _a === void 0 ? void 0 : _a.files);
        if (files === null || files === void 0 ? void 0 : files.length) {
            onFilesAdded === null || onFilesAdded === void 0 ? void 0 : onFilesAdded(files);
            // Reseting the input type to allow selecting the same file(s)
            e.currentTarget.type = '';
            e.currentTarget.type = inputType;
        }
    };
    const getFilesWebkitDataTransferItems = (dataTransferItems) => {
        const traverseFileTreePromise = (item, path = '') => new Promise(resolve => {
            if (item.isFile) {
                const fileEntry = item;
                fileEntry.file(file => {
                    ;
                    file.filepath = path + file.name; // Save full path with type casting
                    files.push(file);
                    resolve([file]);
                });
            }
            else if (item.isDirectory) {
                const dirEntry = item;
                const dirReader = dirEntry.createReader();
                dirReader.readEntries(entries => {
                    const entriesPromises = entries.map(entry => traverseFileTreePromise(entry, path + dirEntry.name + '/'));
                    resolve(Promise.all(entriesPromises).then(entries => [].concat(...entries)));
                });
            }
        });
        const files = [];
        return new Promise((resolve, reject) => {
            const entriesPromises = [];
            for (let i = 0; i < dataTransferItems.length; i++) {
                const item = dataTransferItems[i].webkitGetAsEntry();
                if (item)
                    entriesPromises.push(traverseFileTreePromise(item));
            }
            Promise.all(entriesPromises)
                .then(() => resolve(files))
                .catch(reject);
        });
    };
    const handleDrop = (e) => {
        var _a;
        e.preventDefault();
        if (disabled)
            return undefined;
        if (selectFolderOnDrop) {
            const items = e.dataTransfer.items;
            getFilesWebkitDataTransferItems(items).then(files => (files === null || files === void 0 ? void 0 : files.length) && (onFilesAdded === null || onFilesAdded === void 0 ? void 0 : onFilesAdded(files)));
            return undefined;
        }
        const files = ((_a = e.dataTransfer) === null || _a === void 0 ? void 0 : _a.files) ? processFiles(e.dataTransfer.files) : [];
        if (files === null || files === void 0 ? void 0 : files.length)
            onFilesAdded === null || onFilesAdded === void 0 ? void 0 : onFilesAdded(files);
    };
    return ((0, jsx_runtime_1.jsxs)(DragAndDropBase, Object.assign({ className: (0, utils_1.cs)('vui-drag-and-drop', className), cursor: disabled ? 'not-allowed' : 'pointer', onDragEnter: e => e.preventDefault(), onDragOver: e => e.preventDefault(), onDrop: e => handleDrop(e), opacity: disabled ? 0.5 : 1, p: 5, pointerEvents: disabled ? 'none' : 'auto', ref: ref }, styles, rest, { children: [children || ((0, jsx_runtime_1.jsxs)(t_1.default, { display: "flex", children: [(0, jsx_runtime_1.jsx)(icon_1.default, { mr: 1, name: icon }), text] })), (0, jsx_runtime_1.jsx)("input", Object.assign({ accept: accept, className: "vui-drag-and-drop-hidden-input", multiple: multiple, onChange: handleFileInput, ref: fileInput, style: { display: 'none' }, type: inputType }, (selectFolderOnClick && { webkitdirectory: 'true', directory: 'true' })))] })));
});
exports.DragAndDrop.displayName = 'DragAndDrop';
exports.default = exports.DragAndDrop;
//# sourceMappingURL=dragAndDrop.js.map