"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const property = require("../../../../../../@lit_reactive-element@1.6.3/node_modules/@lit/reactive-element/decorators/property.js");
require("../../../../../../@lit_reactive-element@1.6.3/node_modules/@lit/reactive-element/decorators/query-assigned-elements.js");
const three_module = require("../../../../../../three@0.154.0/node_modules/three/build/three.module.js");
const modelViewerBase = require("../model-viewer-base.js");
/* @license
 * Copyright 2019 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 __decorate = globalThis && globalThis.__decorate || function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1; i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const MILLISECONDS_PER_SECOND = 1e3;
const $changeAnimation = Symbol("changeAnimation");
const $paused = Symbol("paused");
const DEFAULT_PLAY_OPTIONS = {
  repetitions: Infinity,
  pingpong: false
};
const AnimationMixin = (ModelViewerElement) => {
  var _a;
  class AnimationModelViewerElement extends ModelViewerElement {
    constructor(...args) {
      super(args);
      this.autoplay = false;
      this.animationName = void 0;
      this.animationCrossfadeDuration = 300;
      this[_a] = true;
      this[modelViewerBase.$scene].subscribeMixerEvent("loop", (e) => {
        const count = e.action._loopCount;
        this.dispatchEvent(new CustomEvent("loop", { detail: { count } }));
      });
      this[modelViewerBase.$scene].subscribeMixerEvent("finished", () => {
        this[$paused] = true;
        this.dispatchEvent(new CustomEvent("finished"));
      });
    }
    /**
     * Returns an array
     */
    get availableAnimations() {
      if (this.loaded) {
        return this[modelViewerBase.$scene].animationNames;
      }
      return [];
    }
    get duration() {
      return this[modelViewerBase.$scene].duration;
    }
    get paused() {
      return this[$paused];
    }
    get currentTime() {
      return this[modelViewerBase.$scene].animationTime;
    }
    set currentTime(value) {
      this[modelViewerBase.$scene].animationTime = value;
      this[modelViewerBase.$needsRender]();
    }
    get timeScale() {
      return this[modelViewerBase.$scene].animationTimeScale;
    }
    set timeScale(value) {
      this[modelViewerBase.$scene].animationTimeScale = value;
    }
    pause() {
      if (this[$paused]) {
        return;
      }
      this[$paused] = true;
      this.dispatchEvent(new CustomEvent("pause"));
    }
    play(options) {
      if (this.availableAnimations.length > 0) {
        this[$paused] = false;
        this[$changeAnimation](options);
        this.dispatchEvent(new CustomEvent("play"));
      }
    }
    [(_a = $paused, modelViewerBase.$onModelLoad)]() {
      super[modelViewerBase.$onModelLoad]();
      this[$paused] = true;
      if (this.animationName != null) {
        this[$changeAnimation]();
      }
      if (this.autoplay) {
        this.play();
      }
    }
    [modelViewerBase.$tick](_time, delta) {
      super[modelViewerBase.$tick](_time, delta);
      if (this[$paused] || !this[modelViewerBase.$getModelIsVisible]() && !this[modelViewerBase.$renderer].isPresenting) {
        return;
      }
      this[modelViewerBase.$scene].updateAnimation(delta / MILLISECONDS_PER_SECOND);
      this[modelViewerBase.$needsRender]();
    }
    updated(changedProperties) {
      super.updated(changedProperties);
      if (changedProperties.has("autoplay") && this.autoplay) {
        this.play();
      }
      if (changedProperties.has("animationName")) {
        this[$changeAnimation]();
      }
    }
    [$changeAnimation](options = DEFAULT_PLAY_OPTIONS) {
      var _b;
      const repetitions = (_b = options.repetitions) !== null && _b !== void 0 ? _b : Infinity;
      const mode = options.pingpong ? three_module.LoopPingPong : repetitions === 1 ? three_module.LoopOnce : three_module.LoopRepeat;
      this[modelViewerBase.$scene].playAnimation(this.animationName, this.animationCrossfadeDuration / MILLISECONDS_PER_SECOND, mode, repetitions);
      if (this[$paused]) {
        this[modelViewerBase.$scene].updateAnimation(0);
        this[modelViewerBase.$needsRender]();
      }
    }
  }
  __decorate([
    property.property({ type: Boolean })
  ], AnimationModelViewerElement.prototype, "autoplay", void 0);
  __decorate([
    property.property({ type: String, attribute: "animation-name" })
  ], AnimationModelViewerElement.prototype, "animationName", void 0);
  __decorate([
    property.property({ type: Number, attribute: "animation-crossfade-duration" })
  ], AnimationModelViewerElement.prototype, "animationCrossfadeDuration", void 0);
  return AnimationModelViewerElement;
};
exports.AnimationMixin = AnimationMixin;
//# sourceMappingURL=animation.js.map
