import { Vector2, SRGBColorSpace, LinearSRGBColorSpace } from "../../../../../../../three@0.154.0/node_modules/three/build/three.module.js";
import { $threeTexture } from "./image.js";
import { Texture } from "./texture.js";
/* @license
 * Copyright 2020 Google LLC. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var _a, _b, _c;
const $texture = Symbol("texture");
const $transform = Symbol("transform");
const $materials = Symbol("materials");
const $usage = Symbol("usage");
const $onUpdate = Symbol("onUpdate");
const $activeVideo = Symbol("activeVideo");
var TextureUsage;
(function(TextureUsage2) {
  TextureUsage2[TextureUsage2["Base"] = 0] = "Base";
  TextureUsage2[TextureUsage2["MetallicRoughness"] = 1] = "MetallicRoughness";
  TextureUsage2[TextureUsage2["Normal"] = 2] = "Normal";
  TextureUsage2[TextureUsage2["Occlusion"] = 3] = "Occlusion";
  TextureUsage2[TextureUsage2["Emissive"] = 4] = "Emissive";
  TextureUsage2[TextureUsage2["Clearcoat"] = 5] = "Clearcoat";
  TextureUsage2[TextureUsage2["ClearcoatRoughness"] = 6] = "ClearcoatRoughness";
  TextureUsage2[TextureUsage2["ClearcoatNormal"] = 7] = "ClearcoatNormal";
  TextureUsage2[TextureUsage2["SheenColor"] = 8] = "SheenColor";
  TextureUsage2[TextureUsage2["SheenRoughness"] = 9] = "SheenRoughness";
  TextureUsage2[TextureUsage2["Transmission"] = 10] = "Transmission";
  TextureUsage2[TextureUsage2["Thickness"] = 11] = "Thickness";
  TextureUsage2[TextureUsage2["Specular"] = 12] = "Specular";
  TextureUsage2[TextureUsage2["SpecularColor"] = 13] = "SpecularColor";
  TextureUsage2[TextureUsage2["Iridescence"] = 14] = "Iridescence";
  TextureUsage2[TextureUsage2["IridescenceThickness"] = 15] = "IridescenceThickness";
  TextureUsage2[TextureUsage2["Anisotropy"] = 16] = "Anisotropy";
})(TextureUsage || (TextureUsage = {}));
class TextureInfo {
  constructor(onUpdate, usage, threeTexture, material) {
    this[_a] = null;
    this[_b] = {
      rotation: 0,
      scale: new Vector2(1, 1),
      offset: new Vector2(0, 0)
    };
    this[_c] = false;
    if (threeTexture) {
      this[$transform].rotation = threeTexture.rotation;
      this[$transform].scale.copy(threeTexture.repeat);
      this[$transform].offset.copy(threeTexture.offset);
      this[$texture] = new Texture(onUpdate, threeTexture);
    }
    this[$onUpdate] = onUpdate;
    this[$materials] = material;
    this[$usage] = usage;
  }
  get texture() {
    return this[$texture];
  }
  setTexture(texture) {
    var _d, _e;
    const threeTexture = texture != null ? texture.source[$threeTexture] : null;
    const oldTexture = (_d = this[$texture]) === null || _d === void 0 ? void 0 : _d.source[$threeTexture];
    if (oldTexture != null && oldTexture.isVideoTexture) {
      this[$activeVideo] = false;
    } else if ((_e = this[$texture]) === null || _e === void 0 ? void 0 : _e.source.animation) {
      this[$texture].source.animation.removeEventListener("enterFrame", this[$onUpdate]);
    }
    this[$texture] = texture;
    if (threeTexture != null && threeTexture.isVideoTexture) {
      const element = threeTexture.image;
      this[$activeVideo] = true;
      if (element.requestVideoFrameCallback != null) {
        const update = () => {
          if (!this[$activeVideo]) {
            return;
          }
          this[$onUpdate]();
          element.requestVideoFrameCallback(update);
        };
        element.requestVideoFrameCallback(update);
      } else {
        const update = () => {
          if (!this[$activeVideo]) {
            return;
          }
          this[$onUpdate]();
          requestAnimationFrame(update);
        };
        requestAnimationFrame(update);
      }
    } else if ((texture === null || texture === void 0 ? void 0 : texture.source.animation) != null) {
      texture.source.animation.addEventListener("enterFrame", this[$onUpdate]);
    }
    let colorSpace = SRGBColorSpace;
    if (this[$materials]) {
      for (const material of this[$materials]) {
        switch (this[$usage]) {
          case TextureUsage.Base:
            material.map = threeTexture;
            break;
          case TextureUsage.MetallicRoughness:
            colorSpace = LinearSRGBColorSpace;
            material.metalnessMap = threeTexture;
            material.roughnessMap = threeTexture;
            break;
          case TextureUsage.Normal:
            colorSpace = LinearSRGBColorSpace;
            material.normalMap = threeTexture;
            break;
          case TextureUsage.Occlusion:
            colorSpace = LinearSRGBColorSpace;
            material.aoMap = threeTexture;
            break;
          case TextureUsage.Emissive:
            material.emissiveMap = threeTexture;
            break;
          case TextureUsage.Clearcoat:
            material.clearcoatMap = threeTexture;
            break;
          case TextureUsage.ClearcoatRoughness:
            material.clearcoatRoughnessMap = threeTexture;
            break;
          case TextureUsage.ClearcoatNormal:
            material.clearcoatNormalMap = threeTexture;
            break;
          case TextureUsage.SheenColor:
            material.sheenColorMap = threeTexture;
            break;
          case TextureUsage.SheenRoughness:
            material.sheenRoughnessMap = threeTexture;
            break;
          case TextureUsage.Transmission:
            material.transmissionMap = threeTexture;
            break;
          case TextureUsage.Thickness:
            material.thicknessMap = threeTexture;
            break;
          case TextureUsage.Specular:
            material.specularIntensityMap = threeTexture;
            break;
          case TextureUsage.SpecularColor:
            material.specularColorMap = threeTexture;
            break;
          case TextureUsage.Iridescence:
            material.iridescenceMap = threeTexture;
            break;
          case TextureUsage.IridescenceThickness:
            material.iridescenceThicknessMap = threeTexture;
            break;
          case TextureUsage.Anisotropy:
            material.anisotropyMap = threeTexture;
            break;
        }
        material.needsUpdate = true;
      }
    }
    if (threeTexture) {
      threeTexture.colorSpace = colorSpace;
      threeTexture.rotation = this[$transform].rotation;
      threeTexture.repeat = this[$transform].scale;
      threeTexture.offset = this[$transform].offset;
    }
    this[$onUpdate]();
  }
}
_a = $texture, _b = $transform, _c = $activeVideo;
export {
  $materials,
  $usage,
  TextureInfo,
  TextureUsage
};
//# sourceMappingURL=texture-info.js.map
