import {CUSTOM_DRAG_TYPE as $2991e2e71ca29774$export$fd9f9fc120c5402d, DROP_OPERATION as $2991e2e71ca29774$export$60b7b4bcf3903d8e, GENERIC_TYPE as $2991e2e71ca29774$export$f8fc6581787339b3, NATIVE_DRAG_TYPES as $2991e2e71ca29774$export$4a7729b856e9a690} from "./constants.mjs";
import {getInteractionModality as $8f5a2122b0992be3$export$630ff653c5ada6a9, useInteractionModality as $8f5a2122b0992be3$export$98e20ec92f614cfe} from "../interactions/useFocusVisible.mjs";

/*
 * Copyright 2020 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */ 

const $d40e85a29b831dd6$export$dfdf5deeaf27473f = new WeakMap();
const $d40e85a29b831dd6$export$990fced5dfac2637 = Symbol();
function $d40e85a29b831dd6$export$3093291712f09a77(state) {
    let { id: id } = $d40e85a29b831dd6$export$dfdf5deeaf27473f.get(state) || {};
    if (!id) throw new Error('Droppable item outside a droppable collection');
    return id;
}
function $d40e85a29b831dd6$export$7e397efd01d3db27(state) {
    let { ref: ref } = $d40e85a29b831dd6$export$dfdf5deeaf27473f.get(state) || {};
    if (!ref) throw new Error('Droppable item outside a droppable collection');
    return ref;
}
function $d40e85a29b831dd6$export$e1d41611756c6326(items) {
    let types = new Set();
    for (let item of items)for (let type of Object.keys(item))types.add(type);
    return types;
}
function $d40e85a29b831dd6$var$mapModality(modality) {
    if (!modality) modality = 'virtual';
    if (modality === 'pointer') modality = 'virtual';
    if (modality === 'virtual' && typeof window !== 'undefined' && 'ontouchstart' in window) modality = 'touch';
    return modality;
}
function $d40e85a29b831dd6$export$49bac5d6d4b352ea() {
    return $d40e85a29b831dd6$var$mapModality((0, $8f5a2122b0992be3$export$98e20ec92f614cfe)());
}
function $d40e85a29b831dd6$export$1fb2158d224b542c() {
    return $d40e85a29b831dd6$var$mapModality((0, $8f5a2122b0992be3$export$630ff653c5ada6a9)());
}
function $d40e85a29b831dd6$export$f9c1490890ddd063(dataTransfer, items) {
    // The data transfer API doesn't support more than one item of a given type at once.
    // In addition, only a small set of types are supported natively for transfer between applications.
    // We allow for both multiple items, as well as multiple representations of a single item.
    // In order to make our API work with the native API, we serialize all items to JSON and
    // store as a single native item. We only need to do this if there is more than one item
    // of the same type, or if an item has more than one representation. Otherwise the native
    // API is sufficient.
    //
    // The DataTransferItemList API also theoretically supports adding files, which would enable
    // dragging binary data out of the browser onto the user's desktop for example. Unfortunately,
    // this does not currently work in any browser, so it is not currently supported by our API.
    // See e.g. https://bugs.chromium.org/p/chromium/issues/detail?id=438479.
    let groupedByType = new Map();
    let needsCustomData = false;
    let customData = [];
    for (let item of items){
        let types = Object.keys(item);
        if (types.length > 1) needsCustomData = true;
        let dataByType = {};
        for (let type of types){
            let typeItems = groupedByType.get(type);
            if (!typeItems) {
                typeItems = [];
                groupedByType.set(type, typeItems);
            } else needsCustomData = true;
            let data = item[type];
            dataByType[type] = data;
            typeItems.push(data);
        }
        customData.push(dataByType);
    }
    for (let [type, items] of groupedByType)if ((0, $2991e2e71ca29774$export$4a7729b856e9a690).has(type)) {
        // Only one item of a given type can be set on a data transfer.
        // Join all of the items together separated by newlines.
        let data = items.join('\n');
        dataTransfer.items.add(data, type);
    } else // Set data to the first item so we have access to the list of types.
    dataTransfer.items.add(items[0], type);
    if (needsCustomData) {
        let data = JSON.stringify(customData);
        dataTransfer.items.add(data, (0, $2991e2e71ca29774$export$fd9f9fc120c5402d));
    }
}
class $d40e85a29b831dd6$export$7f04ce188c91447c {
    constructor(dataTransfer){
        this.types = new Set();
        let hasFiles = false;
        for (let item of dataTransfer.items)if (item.type !== (0, $2991e2e71ca29774$export$fd9f9fc120c5402d)) {
            if (item.kind === 'file') hasFiles = true;
            if (item.type) this.types.add(item.type);
            else // Files with unknown types or extensions that don't map to a known mime type
            // are sometimes exposed as an empty string by the browser. Map to a generic
            // mime type instead. Note that this could also be a directory as there's no
            // way to determine if something is a file or directory until drop.
            this.types.add((0, $2991e2e71ca29774$export$f8fc6581787339b3));
        }
        // In Safari, when dragging files, the dataTransfer.items list is empty, but dataTransfer.types contains "Files".
        // Unfortunately, this doesn't tell us what types of files the user is dragging, so we need to assume that any
        // type the user checks for is included. See https://bugs.webkit.org/show_bug.cgi?id=223517.
        this.includesUnknownTypes = !hasFiles && dataTransfer.types.includes('Files');
    }
    has(type) {
        if (this.includesUnknownTypes || type === $d40e85a29b831dd6$export$990fced5dfac2637 && this.types.has((0, $2991e2e71ca29774$export$f8fc6581787339b3))) return true;
        return typeof type === 'string' && this.types.has(type);
    }
}
function $d40e85a29b831dd6$export$d9e760437831f8b3(dataTransfer) {
    let items = [];
    if (!dataTransfer) return items;
    // If our custom drag type is available, use that. This is a JSON serialized
    // representation of all items in the drag, set when there are multiple items
    // of the same type, or an individual item has multiple representations.
    let hasCustomType = false;
    if (dataTransfer.types.includes((0, $2991e2e71ca29774$export$fd9f9fc120c5402d))) try {
        let data = dataTransfer.getData((0, $2991e2e71ca29774$export$fd9f9fc120c5402d));
        let parsed = JSON.parse(data);
        for (let item of parsed)items.push({
            kind: 'text',
            types: new Set(Object.keys(item)),
            getText: (type)=>Promise.resolve(item[type])
        });
        hasCustomType = true;
    } catch  {
    // ignore
    }
    // Otherwise, map native drag items to items of a single representation.
    if (!hasCustomType) {
        let stringItems = new Map();
        for (let item of dataTransfer.items){
            if (item.kind === 'string') // The data for all formats must be read here because the data transfer gets
            // cleared out after the event handler finishes. If the item has an empty string
            // as a type, the mime type is unknown. Map to a generic mime type instead.
            stringItems.set(item.type || (0, $2991e2e71ca29774$export$f8fc6581787339b3), dataTransfer.getData(item.type));
            else if (item.kind === 'file') {
                // Despite the name, webkitGetAsEntry is also implemented in Firefox and Edge.
                // In the future, we may use getAsFileSystemHandle instead, but that's currently
                // only implemented in Chrome.
                if (typeof item.webkitGetAsEntry === 'function') {
                    let entry = item.webkitGetAsEntry();
                    // eslint-disable-next-line max-depth
                    if (!entry) continue;
                    // eslint-disable-next-line max-depth
                    if (entry.isFile) items.push($d40e85a29b831dd6$var$createFileItem(item.getAsFile()));
                    else if (entry.isDirectory) items.push($d40e85a29b831dd6$var$createDirectoryItem(entry));
                } else // Assume it's a file.
                items.push($d40e85a29b831dd6$var$createFileItem(item.getAsFile()));
            }
        }
        // All string items are different representations of the same item. There's no way to have
        // multiple string items at once in the current DataTransfer API.
        if (stringItems.size > 0) items.push({
            kind: 'text',
            types: new Set(stringItems.keys()),
            getText: (type)=>Promise.resolve(stringItems.get(type))
        });
    }
    return items;
}
function $d40e85a29b831dd6$var$blobToString(blob) {
    if (typeof blob.text === 'function') return blob.text();
    // Safari doesn't have the Blob#text() method yet...
    return new Promise((resolve, reject)=>{
        let reader = new FileReader;
        reader.onload = ()=>{
            resolve(reader.result);
        };
        reader.onerror = reject;
        reader.readAsText(blob);
    });
}
function $d40e85a29b831dd6$var$createFileItem(file) {
    if (!file) throw new Error('No file provided');
    return {
        kind: 'file',
        type: file.type || (0, $2991e2e71ca29774$export$f8fc6581787339b3),
        name: file.name,
        getText: ()=>$d40e85a29b831dd6$var$blobToString(file),
        getFile: ()=>Promise.resolve(file)
    };
}
function $d40e85a29b831dd6$var$createDirectoryItem(entry) {
    return {
        kind: 'directory',
        name: entry.name,
        getEntries: ()=>$d40e85a29b831dd6$var$getEntries(entry)
    };
}
async function* $d40e85a29b831dd6$var$getEntries(item) {
    let reader = item.createReader();
    // We must call readEntries repeatedly because there may be a limit to the
    // number of entries that are returned at once.
    let entries;
    do {
        entries = await new Promise((resolve, reject)=>{
            reader.readEntries(resolve, reject);
        });
        for (let entry of entries){
            if (entry.isFile) {
                let file = await $d40e85a29b831dd6$var$getEntryFile(entry);
                yield $d40e85a29b831dd6$var$createFileItem(file);
            } else if (entry.isDirectory) yield $d40e85a29b831dd6$var$createDirectoryItem(entry);
        }
    }while (entries.length > 0);
}
function $d40e85a29b831dd6$var$getEntryFile(entry) {
    return new Promise((resolve, reject)=>entry.file(resolve, reject));
}
function $d40e85a29b831dd6$export$97fd558bdc44bea1(dropItem) {
    return dropItem.kind === 'text';
}
function $d40e85a29b831dd6$export$a144e1752ebe0aa(dropItem) {
    return dropItem.kind === 'file';
}
function $d40e85a29b831dd6$export$2b40a62bdbe6b2b0(dropItem) {
    return dropItem.kind === 'directory';
}
let $d40e85a29b831dd6$export$6ca6700462636d0b = {
    draggingKeys: new Set()
};
function $d40e85a29b831dd6$export$f2be18a910c0caa6(ref) {
    $d40e85a29b831dd6$export$6ca6700462636d0b.draggingCollectionRef = ref;
}
function $d40e85a29b831dd6$export$72cb63bdda528276(keys) {
    $d40e85a29b831dd6$export$6ca6700462636d0b.draggingKeys = keys;
}
function $d40e85a29b831dd6$export$dac8db29d42db9a1(ref) {
    $d40e85a29b831dd6$export$6ca6700462636d0b.dropCollectionRef = ref;
}
function $d40e85a29b831dd6$export$70936501603e6c57() {
    $d40e85a29b831dd6$export$6ca6700462636d0b = {
        draggingKeys: new Set()
    };
}
function $d40e85a29b831dd6$export$6c10d32b362bfa5f(state) {
    $d40e85a29b831dd6$export$6ca6700462636d0b = state;
}
function $d40e85a29b831dd6$export$78bf638634500fa5(ref) {
    let { draggingCollectionRef: draggingCollectionRef, dropCollectionRef: dropCollectionRef } = $d40e85a29b831dd6$export$6ca6700462636d0b;
    return draggingCollectionRef?.current != null && draggingCollectionRef.current === (ref?.current || dropCollectionRef?.current);
}
let $d40e85a29b831dd6$export$8e6636520ac15722;
function $d40e85a29b831dd6$export$64f52ed7349ddb84(dropEffect) {
    $d40e85a29b831dd6$export$8e6636520ac15722 = dropEffect;
}
let $d40e85a29b831dd6$export$f0130eb70b6347b8 = (0, $2991e2e71ca29774$export$60b7b4bcf3903d8e).none;
function $d40e85a29b831dd6$export$6539bc8c3a0a2d67(o) {
    $d40e85a29b831dd6$export$f0130eb70b6347b8 = o;
}


export {$d40e85a29b831dd6$export$dfdf5deeaf27473f as droppableCollectionMap, $d40e85a29b831dd6$export$990fced5dfac2637 as DIRECTORY_DRAG_TYPE, $d40e85a29b831dd6$export$3093291712f09a77 as getDroppableCollectionId, $d40e85a29b831dd6$export$7e397efd01d3db27 as getDroppableCollectionRef, $d40e85a29b831dd6$export$e1d41611756c6326 as getTypes, $d40e85a29b831dd6$export$49bac5d6d4b352ea as useDragModality, $d40e85a29b831dd6$export$1fb2158d224b542c as getDragModality, $d40e85a29b831dd6$export$f9c1490890ddd063 as writeToDataTransfer, $d40e85a29b831dd6$export$7f04ce188c91447c as DragTypes, $d40e85a29b831dd6$export$d9e760437831f8b3 as readFromDataTransfer, $d40e85a29b831dd6$export$97fd558bdc44bea1 as isTextDropItem, $d40e85a29b831dd6$export$a144e1752ebe0aa as isFileDropItem, $d40e85a29b831dd6$export$2b40a62bdbe6b2b0 as isDirectoryDropItem, $d40e85a29b831dd6$export$6ca6700462636d0b as globalDndState, $d40e85a29b831dd6$export$f2be18a910c0caa6 as setDraggingCollectionRef, $d40e85a29b831dd6$export$72cb63bdda528276 as setDraggingKeys, $d40e85a29b831dd6$export$dac8db29d42db9a1 as setDropCollectionRef, $d40e85a29b831dd6$export$70936501603e6c57 as clearGlobalDnDState, $d40e85a29b831dd6$export$6c10d32b362bfa5f as setGlobalDnDState, $d40e85a29b831dd6$export$78bf638634500fa5 as isInternalDropOperation, $d40e85a29b831dd6$export$8e6636520ac15722 as globalDropEffect, $d40e85a29b831dd6$export$64f52ed7349ddb84 as setGlobalDropEffect, $d40e85a29b831dd6$export$f0130eb70b6347b8 as globalAllowedDropOperations, $d40e85a29b831dd6$export$6539bc8c3a0a2d67 as setGlobalAllowedDropOperations};
//# sourceMappingURL=utils.mjs.map
