/**
 * Provides types for the experimental File System Access API available in Chromium-based browsers
 * See: https://wicg.github.io/file-system-access/
 */
export const fileAccessContext = window;
export const hasFileAccessAPI = globalThis.DataTransferItem &&
    'getAsFileSystemHandle' in DataTransferItem.prototype;
/**
 * Attempts to acquire a FileSystemHandle from a DataTransferItem if running in a
 * browser with File Access API, otherwise returns null
 * @param item
 */
export async function tryGetAsFilesystemHandle(item) {
    if ('getAsFileSystemHandle' in item) {
        return item.getAsFileSystemHandle();
    }
    return Promise.resolve(null);
}
export async function tryQueryPermission(handle, options) {
    if ('queryPermission' in handle) {
        return handle.queryPermission(options);
    }
    return 'denied';
}
export async function tryGetFile(handle) {
    if ('getFile' in handle) {
        return handle.getFile();
    }
    return handle;
}
/**
 * Convert the File Access API style accept object to a flat string suitable for <input type=file>
 * @param types Array of FilePickerType objects
 */
export function flattenAcceptTypes(types) {
    // Input:
    // [
    //   {
    //     description: 'Lossless image',
    //     accept: {
    //       'image/png': ['*.png'],
    //       'image/bmp': ['*.bmp', '*.dib'],
    //     },
    //   },
    //   {
    //     description: 'Lossy image',
    //     accept: {
    //       'image/jpeg': ['*.jpg', '*.jpeg', '*.jpe', '*.jfif'],
    //       'image/gif': ['*.gif'],
    //     },
    //   },
    // ]
    // Output:
    // 'image/png,*.png,image/bmp,*.bmp,*.dib,image/jpeg,*.jpg,*.jpeg,*.jpe,*.jfif,image/gif,*.gif'
    return types
        ?.map(type => Object.entries(type.accept)
        .map(([mimetype, extensions]) => `${mimetype},${extensions.join()}`)
        .join())
        ?.join();
}
//# sourceMappingURL=file-access-api.js.map