import { bytesToSize, getUrl, getUrl2 } from 'educoder';
const $ = window.$
import showdown from 'showdown'
import showdownKatex from './showdown-katex/showdown-katex'

export function isImageExtension(fileName) {
    return fileName ? !!(fileName.match(/.(jpg|jpeg|png|gif)$/i)) : false
}
const katex = showdownKatex()
const converter = new showdown.Converter({
    extensions: [katex]
})

export function markdownToHTML(oldContent) {
    if (oldContent) {
        return converter.makeHtml(oldContent)
    }
    return oldContent
}
function _doDownload(options) {
    $.fileDownload(getUrl() + "/api" + options.url, {
        successCallback: options.successCallback,
        failCallback: options.failCallback
    });
}
export function downloadFile(options) {
    if ($.fileDownload) {
        _doDownload(options)
    } else {
        const _url_origin = getUrl2()
        $.getScript(
            `${_url_origin}/javascripts/download/jquery.fileDownload.min.js`,
            (data, textStatus, jqxhr) => {
                _doDownload(options)
            });
    }

}

export function appendFileSizeToUploadFile(item) {
    return `${item.title}${uploadNameSizeSeperator}${item.filesize}`
}
export function appendFileSizeToUploadFileAll(fileList) {
    return fileList.map(item => {
        if (item.name.indexOf(uploadNameSizeSeperator) == -1) {
            return Object.assign({}, item, { name: `${item.name}${uploadNameSizeSeperator}${bytesToSize(item.size)}` })
        }
        return item
    })
}
export const uploadNameSizeSeperator = '　　'

export const sortDirections = ["ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend",
    "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend",
    "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend",
    "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend",]