import * as path from 'path'

const fg = require('fast-glob')

import { Converter } from 'showdown'

export const addAssembly = (item) => `${item}/cad/**/Global*.+(SLDASM)`

export const md2html = (content) => {
    let converter = new Converter({ tables: true });
    converter.setOption('literalMidWordUnderscores', 'true');
    return converter.makeHtml(content);
}

export const forward_slash = (path) => {
    const isExtendedLengthPath = /^\\\\\?\\/.test(path);
    const hasNonAscii = /[^\u0000-\u0080]+/.test(path); // eslint-disable-line no-control-regex

    if (isExtendedLengthPath || hasNonAscii) {
        return path;
    }

    return path.replace(/\\/g, '/');
};

export const files = (dir, glob) => fg.sync(glob, {
    dot: true,
    cwd: dir,
    absolute: true
})

export const MainAssembly = (dir) => {
    const mains = files(dir, '**/cad/**/*Global*.+(SLDASM)');
    return mains[0];
}

export const file_path_with_ext = (file, ext) => {
    const parts = path.parse(file);
    return path.join(parts.dir, parts.name + '.' + ext);
}

export const unique_by = (arr, key) => {
    return [...new Map(arr.map(item => [item[key], item])).values()]
}

