import { REVISION } from 'three';

/**
 * DDS Texture Extension
 *
 * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_texture_dds
 *
 */
export default class GLTFTextureDDSExtension {
    /**
     *
     * @param {GLTFParser} parser
     * @param {DDSLoader} ddsLoader
     */
    constructor(parser, ddsLoader) {
        this.name = 'MSFT_texture_dds';

        /**
         *
         * @type {GLTFParser}
         */
        this.parser = parser;
        this.ddsLoader = ddsLoader;
    }

    loadTexture(textureIndex) {
        const json = this.parser.json;
        const textureDef = json.textures[textureIndex];
        if (!textureDef.extensions || !textureDef.extensions[this.name]) {
            return null;
        }
        const extensionDef = textureDef.extensions[this.name];
        const source_index = extensionDef.source;

        const source = json.images[source_index];

        if (REVISION <= 137) {

            return this.parser.loadTextureImage(textureIndex, source, this.ddsLoader);

        } else {
            // API of GLTFLoader has changed after 137
            return this.parser.loadTextureImage(textureIndex, source_index, this.ddsLoader);
        }
    }
}
