/**
 * @module utils/BrandedFileLoader
 */
'use strict';

import {getConfig} from '../../utils';

/**
 * @alias utils/BrandedFileLoader:BrandedFileLoader
 */
export default class BrandedFileLoader {
    /**
     * a branded folder's structure should look like, put the rebranded file with or without locale name in branded
     * folders with a same name:
     *
     * - folderName
     *   - default
     *   - 1210
     *   - 3610
     *   - 7310
     *   - 7710
     *
     * @param requireCtx
     */
    constructor(requireCtx) {
        this.require = requireCtx;
        this.brandId = getConfig().brandId;
        this.locale = getConfig().userLanguage;
        this.defaultLocale = 'en_US';
    }

    require: any;
    brandId: any;
    locale: any;
    defaultLocale: any;

    /**
     * @param filePath
     * @return {string}
     */
    getFile(filePath) {
        return this.getFileInfo(filePath).file;
    }

    /**
     * @param {string} filePath
     * @return {{file: *, requirePath: *}}
     */
    getFileInfo(filePath) {
        let requirePath = this.getRequirePath(filePath);
        return {
            file: this.require(requirePath),
            requirePath
        };
    }

    /**
     * @param filePath
     * @return {string}
     */
    getRequirePath(filePath) {
        let fileArr = filePath.split('/');
        let fileName = fileArr.pop();
        let extReg = /(.+)(\.\S+$)/;
        let i18nFileFileName = fileName.replace(extReg, `$1-${this.locale}$2`);
        let i18nFilePath = []
            .concat(fileArr)
            .concat([i18nFileFileName] as never)
            .join('/');

        let enUSFileName = fileName.replace(extReg, `$1-${this.defaultLocale}$2`);
        let enUSFilePath = []
            .concat(fileArr)
            .concat([enUSFileName] as never)
            .join('/');

        let brandedI18nFilePath = `./${this.brandId}/${i18nFilePath}`;
        let brandedEnUSFilePath = `./${this.brandId}/${enUSFilePath}`;
        let defaultEnUsFilePath = `./default/${enUSFilePath}`;
        let brandedFilePath = `./${this.brandId}/${filePath}`;
        let defaultI18nFilePath = `./default/${i18nFilePath}`;
        let defaultFilePath = `./default/${filePath}`;

        let fileList = this.require.keys();

        let hasFile = file => fileList.indexOf(file) >= 0;

        // require file path like : ~otherPath/7310/logo-fr_CA.png
        if (hasFile(brandedI18nFilePath)) {
            return brandedI18nFilePath;
        }

        // require file path like : ~otherPath/7310/logo-en_US.png
        if (hasFile(brandedEnUSFilePath)) {
            return brandedEnUSFilePath;
        }

        // require file path like : ~otherPath/7310/logo.png
        if (hasFile(defaultI18nFilePath)) {
            return defaultI18nFilePath;
        }

        // require file path like : ~otherPath/default/logo-en_US.png
        if (hasFile(defaultEnUsFilePath)) {
            return defaultEnUsFilePath;
        }

        // require file path like : ~otherPath/7310/logo.png
        if (hasFile(brandedFilePath)) {
            return brandedFilePath;
        }

        // require file path like : ~otherPath/1210/logo.png
        return defaultFilePath;
    }
}
