import { defaultMimeType } from './content-type-parser.js';
import { isPromise } from './type-guards.js';
export async function getContentType({ bytes, path, contentTypeParser, log, defaultContentType = 'application/octet-stream', filename: filenameParam }) {
    let contentType;
    if (contentTypeParser != null) {
        try {
            let fileName;
            if (filenameParam == null) {
                fileName = path?.[path.length - 1]?.trim();
                fileName = (fileName === '' || fileName?.split('.').length === 1) ? undefined : fileName;
            }
            else {
                fileName = filenameParam;
            }
            const parsed = contentTypeParser(bytes, fileName);
            if (isPromise(parsed)) {
                const result = await parsed;
                if (result != null) {
                    contentType = result;
                }
            }
            else if (parsed != null) {
                contentType = parsed;
            }
            log.trace('contentTypeParser returned %s for file with name %s', contentType, fileName);
        }
        catch (err) {
            log.error('error parsing content type - %e', err);
        }
    }
    if (contentType === defaultMimeType) {
        // if the content type is the default in our content-type-parser, instead, set it to the default content type provided to this function.
        contentType = defaultContentType;
    }
    return contentType ?? defaultContentType;
}
//# sourceMappingURL=get-content-type.js.map