import {$log} from "ts-log-debug";
import {DirectoryHash} from "./directory-hash";
import * as path from "path";

var vfs = require('vinyl-fs');
var vinylMd5 = require('gulp-vinyl-md5');

export class FileUtils {

    static async collectHashes(root: string): Promise<DirectoryHash>{
        var map: DirectoryHash = { };
        return new Promise<DirectoryHash>(res=> {
            vfs.src([`${root}/**/*.{fbx,obj,jpg,jpeg,png}`])
                .pipe(vinylMd5())
                .on('data', (data: any)=>{
                    map[data.md5Value] = data.path.replace(root + path.sep, '');
                })
                .on('end', ()=>{
                    res(map);
                });
        });
    }
}

const SUPPORTED_SOURCE_ASSET = ['fbx', 'obj'];

export function isSourceAsset(file: string): boolean {
    for (const ext of SUPPORTED_SOURCE_ASSET) {
        if (file.endsWith(`.${ext}`)) {
            return true;
        }
    }
    return false;
}

export function assetType(file: string): string {
    return path.extname(file).slice(1);
}