import {CUSTOM_DRAG_TYPE as $a5876944c9299b39$export$fd9f9fc120c5402d, DROP_OPERATION as $a5876944c9299b39$export$60b7b4bcf3903d8e, GENERIC_TYPE as $a5876944c9299b39$export$f8fc6581787339b3, NATIVE_DRAG_TYPES as $a5876944c9299b39$export$4a7729b856e9a690} from "./constants.js";
import {getInteractionModality as $b50b1cc8a843ace7$export$630ff653c5ada6a9, useInteractionModality as $b50b1cc8a843ace7$export$98e20ec92f614cfe} from "../interactions/useFocusVisible.js";

/*
 * 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 $a279fa400589a731$export$dfdf5deeaf27473f = new WeakMap();
const $a279fa400589a731$export$990fced5dfac2637 = Symbol();
function $a279fa400589a731$export$3093291712f09a77(state) {
    let { id: id } = $a279fa400589a731$export$dfdf5deeaf27473f.get(state) || {};
    if (!id) throw new Error('Droppable item outside a droppable collection');
    return id;
}
function $a279fa400589a731$export$7e397efd01d3db27(state) {
    let { ref: ref } = $a279fa400589a731$export$dfdf5deeaf27473f.get(state) || {};
    if (!ref) throw new Error('Droppable item outside a droppable collection');
    return ref;
}
function $a279fa400589a731$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 $a279fa400589a731$var$mapModality(modality) {
    if (!modality) modality = 'virtual';
    if (modality === 'pointer') modality = 'virtual';
    if (modality === 'virtual' && typeof window !== 'undefined' && typeof window.matchMedia === 'function' && window.matchMedia('(pointer: coarse)').matches) modality = 'touch';
    return modality;
}
function $a279fa400589a731$export$49bac5d6d4b352ea() {
    return $a279fa400589a731$var$mapModality((0, $b50b1cc8a843ace7$export$98e20ec92f614cfe)());
}
function $a279fa400589a731$export$1fb2158d224b542c() {
    return $a279fa400589a731$var$mapModality((0, $b50b1cc8a843ace7$export$630ff653c5ada6a9)());
}
function $a279fa400589a731$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, $a5876944c9299b39$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, $a5876944c9299b39$export$fd9f9fc120c5402d));
    }
}
class $a279fa400589a731$export$7f04ce188c91447c {
    has(type) {
        if (Array.isArray(type)) return type.some((t)=>this.has(t));
        if (this.includesUnknownTypes || type === $a279fa400589a731$export$990fced5dfac2637 && this.types.has((0, $a5876944c9299b39$export$f8fc6581787339b3)) || type === '*/*') return true;
        if (typeof type === 'string') {
            if (type.endsWith('/*')) {
                for (let key of this.types){
                    if (key.startsWith(type.slice(0, -2))) return true;
                }
                return false;
            }
            return this.types.has(type);
        }
        return false;
    }
    constructor(dataTransfer){
        this.types = new Set();
        let hasFiles = false;
        for (let item of dataTransfer.items)if (item.type !== (0, $a5876944c9299b39$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, $a5876944c9299b39$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');
    }
}
function $a279fa400589a731$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, $a5876944c9299b39$export$fd9f9fc120c5402d))) try {
        let data = dataTransfer.getData((0, $a5876944c9299b39$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, $a5876944c9299b39$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($a279fa400589a731$var$createFileItem(item.getAsFile()));
                    else if (entry.isDirectory) items.push($a279fa400589a731$var$createDirectoryItem(entry));
                } else // Assume it's a file.
                items.push($a279fa400589a731$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 $a279fa400589a731$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 $a279fa400589a731$var$createFileItem(file) {
    if (!file) throw new Error('No file provided');
    return {
        kind: 'file',
        type: file.type || (0, $a5876944c9299b39$export$f8fc6581787339b3),
        name: file.name,
        getText: ()=>$a279fa400589a731$var$blobToString(file),
        getFile: ()=>Promise.resolve(file)
    };
}
function $a279fa400589a731$var$createDirectoryItem(entry) {
    return {
        kind: 'directory',
        name: entry.name,
        getEntries: ()=>$a279fa400589a731$var$getEntries(entry)
    };
}
async function* $a279fa400589a731$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 $a279fa400589a731$var$getEntryFile(entry);
                yield $a279fa400589a731$var$createFileItem(file);
            } else if (entry.isDirectory) yield $a279fa400589a731$var$createDirectoryItem(entry);
        }
    }while (entries.length > 0);
}
function $a279fa400589a731$var$getEntryFile(entry) {
    return new Promise((resolve, reject)=>entry.file(resolve, reject));
}
function $a279fa400589a731$export$97fd558bdc44bea1(dropItem) {
    return dropItem.kind === 'text';
}
function $a279fa400589a731$export$a144e1752ebe0aa(dropItem) {
    return dropItem.kind === 'file';
}
function $a279fa400589a731$export$2b40a62bdbe6b2b0(dropItem) {
    return dropItem.kind === 'directory';
}
let $a279fa400589a731$export$6ca6700462636d0b = {
    draggingKeys: new Set()
};
function $a279fa400589a731$export$f2be18a910c0caa6(ref) {
    $a279fa400589a731$export$6ca6700462636d0b.draggingCollectionRef = ref;
}
function $a279fa400589a731$export$72cb63bdda528276(keys) {
    $a279fa400589a731$export$6ca6700462636d0b.draggingKeys = keys;
}
function $a279fa400589a731$export$dac8db29d42db9a1(ref) {
    $a279fa400589a731$export$6ca6700462636d0b.dropCollectionRef = ref;
}
function $a279fa400589a731$export$70936501603e6c57() {
    $a279fa400589a731$export$6ca6700462636d0b = {
        draggingKeys: new Set()
    };
}
function $a279fa400589a731$export$6c10d32b362bfa5f(state) {
    $a279fa400589a731$export$6ca6700462636d0b = state;
}
function $a279fa400589a731$export$78bf638634500fa5(ref) {
    let { draggingCollectionRef: draggingCollectionRef, dropCollectionRef: dropCollectionRef } = $a279fa400589a731$export$6ca6700462636d0b;
    return (draggingCollectionRef === null || draggingCollectionRef === void 0 ? void 0 : draggingCollectionRef.current) != null && draggingCollectionRef.current === ((ref === null || ref === void 0 ? void 0 : ref.current) || (dropCollectionRef === null || dropCollectionRef === void 0 ? void 0 : dropCollectionRef.current));
}
let $a279fa400589a731$export$8e6636520ac15722;
function $a279fa400589a731$export$64f52ed7349ddb84(dropEffect) {
    $a279fa400589a731$export$8e6636520ac15722 = dropEffect;
}
let $a279fa400589a731$export$f0130eb70b6347b8 = (0, $a5876944c9299b39$export$60b7b4bcf3903d8e).none;
function $a279fa400589a731$export$6539bc8c3a0a2d67(o) {
    $a279fa400589a731$export$f0130eb70b6347b8 = o;
}


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