import { __extends } from "tslib";
import { isNullOrUndefined, Obj, isValue, } from "@intelligentgraphics/ig.utilities";
import { State } from "../../State";
import { CacheHolder } from "./CacheHolder";
var MaterialCache = /** @class */ (function (_super) {
    __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 (isNullOrUndefined(basePath) && isNullOrUndefined(hashes)) {
            return;
        }
        var createHashedAbsoluteUrl = function (url) {
            return CacheHolder.createHashedAbsoluteUrl(url, basePath, hashes);
        };
        if (isValue(material.Bump)) {
            material.Bump.Map = createHashedAbsoluteUrl(material.Bump.Map);
        }
        if (isValue(material.Normal)) {
            material.Normal.Map = createHashedAbsoluteUrl(material.Normal.Map);
        }
        if (isValue(material.Diffuse)) {
            material.Diffuse.Map = createHashedAbsoluteUrl(material.Diffuse.Map);
        }
        if (isValue(material.Alpha) && isValue(material.Alpha.Map)) {
            material.Alpha.Map = createHashedAbsoluteUrl(material.Alpha.Map);
        }
        if (isValue(material.Specular) && isValue(material.Specular.Map)) {
            material.Specular.Map = createHashedAbsoluteUrl(material.Specular.Map);
        }
        if (isValue(material.Metalness) && isValue(material.Metalness.Map)) {
            material.Metalness.Map = createHashedAbsoluteUrl(material.Metalness.Map);
        }
        if (isValue(material.Roughness) && 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 = Obj.deepClone(data.material);
        MaterialCache.fixMaterialPaths(materialWithoutHashes, data.basePath);
        var materialWithHashes = Obj.deepClone(data.material);
        MaterialCache.fixMaterialPaths(materialWithHashes, data.basePath, data.hashes);
        materialWithHashes.name = data.name;
        State.API.Materials[data.name] = materialWithoutHashes;
        return this.createNativeMaterial(data.name, materialWithHashes, data.mode);
    };
    return MaterialCache;
}(CacheHolder));
export { MaterialCache };
