import { adoptStyles } from 'lit';
import { last } from '../common/util.js';
export const ChatFileTypeIcons = new Map(Object.entries({
    css: 'file_css',
    csv: 'file_csv',
    doc: 'file_doc',
    docx: 'file_doc',
    htm: 'file_htm',
    html: 'file_html',
    js: 'file_js',
    json: 'file_json',
    pdf: 'file_pdf',
    rtf: 'file_rtf',
    svg: 'file_svg',
    txt: 'file_txt',
    url: 'file_link',
    xls: 'file_xls',
    xlsx: 'file_xls',
    xml: 'file_xml',
    zip: 'file_zip',
    default: 'file_generic',
}));
export function parseAcceptedFileTypes(fileTypes) {
    const types = fileTypes.split(',').map((each) => each.trim().toLowerCase());
    return {
        extensions: new Set(types.filter((t) => t.startsWith('.'))),
        mimeTypes: new Set(types.filter((t) => !t.startsWith('.') && !t.endsWith('/*'))),
        wildcardTypes: new Set(types.filter((t) => t.endsWith('/*')).map((t) => t.slice(0, -2))),
    };
}
export function isAcceptedFileType(file, accepted) {
    if (!(accepted && file)) {
        return true;
    }
    const { extensions, mimeTypes, wildcardTypes } = accepted;
    const fileType = file.type.toLowerCase();
    const fileExtension = `.${last(file.name.split('.'))?.toLowerCase()}`;
    const [fileBaseType] = fileType.split('/');
    return (extensions.has(fileExtension) ||
        mimeTypes.has(fileType) ||
        wildcardTypes.has(fileBaseType));
}
export function getChatAcceptedFiles(event, accepted) {
    return Array.from(event.dataTransfer?.items ?? [])
        .filter((item) => item.kind === 'file' && isAcceptedFileType(item.getAsFile(), accepted))
        .map((item) => item.getAsFile());
}
export function getIconName(fileType) {
    return fileType?.startsWith('image') ? 'attach_image' : 'attach_document';
}
export function createAttachmentURL(attachment) {
    if (attachment.file) {
        return URL.createObjectURL(attachment.file);
    }
    return attachment.url || '';
}
export function getFileExtension(name) {
    const parts = name.split('.');
    return parts.length > 1 ? last(parts) : '';
}
export function isImageAttachment(attachment) {
    if (attachment instanceof File) {
        return attachment.type.startsWith('image/');
    }
    return Boolean(attachment.type === 'image' || attachment.file?.type.startsWith('image/'));
}
export function chatMessageAdoptPageStyles(message) {
    const sheets = [];
    for (const sheet of document.styleSheets) {
        try {
            const constructed = new CSSStyleSheet();
            for (const rule of sheet.cssRules) {
                constructed.insertRule(rule.cssText);
            }
            sheets.push(constructed);
        }
        catch { }
    }
    const ctor = message.constructor;
    adoptStyles(message.shadowRoot, [...ctor.elementStyles, ...sheets]);
}
//# sourceMappingURL=utils.js.map