"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var State_1 = require("../../State");
var CacheHolder_1 = require("./CacheHolder");
var MaterialCache = /** @class */ (function (_super) {
    tslib_1.__extends(MaterialCache, _super);
    function MaterialCache() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    /**
     * Replaces all paths with absolute hashed versions
     *
     *
     * @param {IMaterialData} { material, hashes, basePath }
     */
    MaterialCache.fixMaterialPaths = function (material, basePath, hashes) {
        if (ig_utilities_1.isNullOrUndefined(basePath) && ig_utilities_1.isNullOrUndefined(hashes)) {
            return;
        }
        var createHashedAbsoluteUrl = function (url) {
            return CacheHolder_1.CacheHolder.createHashedAbsoluteUrl(url, basePath, hashes);
        };
        if (ig_utilities_1.isValue(material.Bump)) {
            material.Bump.Map = createHashedAbsoluteUrl(material.Bump.Map);
        }
        if (ig_utilities_1.isValue(material.Normal)) {
            material.Normal.Map = createHashedAbsoluteUrl(material.Normal.Map);
        }
        if (ig_utilities_1.isValue(material.Diffuse)) {
            material.Diffuse.Map = createHashedAbsoluteUrl(material.Diffuse.Map);
        }
        if (ig_utilities_1.isValue(material.Alpha) && ig_utilities_1.isValue(material.Alpha.Map)) {
            material.Alpha.Map = createHashedAbsoluteUrl(material.Alpha.Map);
        }
        if (ig_utilities_1.isValue(material.Specular) && ig_utilities_1.isValue(material.Specular.Map)) {
            material.Specular.Map = createHashedAbsoluteUrl(material.Specular.Map);
        }
        if (ig_utilities_1.isValue(material.Metalness) && ig_utilities_1.isValue(material.Metalness.Map)) {
            material.Metalness.Map = createHashedAbsoluteUrl(material.Metalness.Map);
        }
        if (ig_utilities_1.isValue(material.Roughness) && ig_utilities_1.isValue(material.Roughness.Map)) {
            material.Roughness.Map = createHashedAbsoluteUrl(material.Roughness.Map);
        }
    };
    MaterialCache.prototype.createIdentifier = function (data) {
        return data.name;
    };
    MaterialCache.prototype.load = function (data) {
        var materialWithoutHashes = ig_utilities_1.Obj.deepClone(data.material);
        MaterialCache.fixMaterialPaths(materialWithoutHashes, data.basePath);
        var materialWithHashes = ig_utilities_1.Obj.deepClone(data.material);
        MaterialCache.fixMaterialPaths(materialWithHashes, data.basePath, data.hashes);
        materialWithHashes.name = data.name;
        State_1.State.API.Materials[data.name] = materialWithoutHashes;
        return this.createNativeMaterial(data.name, materialWithHashes, data.mode);
    };
    return MaterialCache;
}(CacheHolder_1.CacheHolder));
exports.MaterialCache = MaterialCache;
