import { E as ElementModel } from "./background.js";
import { l as lodash } from "./index.js";
import { P as utils } from "./watermark.js";
import { P as Promise$1 } from "./base-element.js";
import "vue";
import "./v4.js";
class AudioModel extends ElementModel {
  constructor(data) {
    if (typeof data.duration !== "number" && typeof data.naturalDuration !== "number") {
      throw new Error("Audio \u5143\u7D20\u7F3A\u5C11 duration \u6216 naturalDuration \u5C5E\u6027");
    }
    super(data);
    if (!this.naturalDuration && this.duration) {
      this.naturalDuration = Math.round(this.duration * 1e3);
    }
    this.endTime = Math.min(this.endTime || this.naturalDuration, this.naturalDuration);
    delete this.duration;
  }
}
var model = AudioModel;
var methods = {
  addAudio(url, options) {
    const data = lodash.exports.assign({
      type: "audio",
      volume: 1,
      width: 0,
      height: 0,
      top: -10,
      left: -10,
      url
    }, options);
    return this.addElement(data);
  },
  seekAudio(element, time) {
    if (element && element.type === "audio") {
      element.$currentTime = time;
    }
  },
  async playAudio(element, time) {
    if (!element || element.type !== "audio")
      return;
    await this.$nextTick();
    time && this.seekAudio(element, time);
    this.$set(element, "$playing", true);
  },
  pauseAudio(element, time) {
    if (!element || element.type !== "audio")
      return;
    this.$set(element, "$playing", false);
    time && this.seekAudio(element, time);
  },
  stopAudio(element) {
    this.pauseAudio(element, 0);
  }
};
var template = '<audio\n    ref="audio"\n    crossorigin\n    :src="originUrl || null"\n    style="display: none; width: 0; height: 0; opacity: 0"\n/>\n';
var AudioElement = {
  name: "audio-element",
  template,
  props: ["global", "model", "options", "editor"],
  data() {
    return {
      audio: null
    };
  },
  computed: {
    originUrl() {
      const { model: model2, options } = this;
      return utils.getComputedUrl(model2.url, options.fitCrossOrigin);
    }
  },
  watch: {
    "model.$currentTime"(time) {
      if (this.audio) {
        this.audio.currentTime = (time || 0) / 1e3;
      }
    },
    "model.volume"(volume) {
      if (this.audio) {
        this.audio.volume = volume;
      }
    },
    "model.$playing"(playing) {
      if (!this.audio)
        return;
      if (playing) {
        this.audio.volume = this.model.volume;
        this.audio.play();
      } else {
        this.audio.pause();
      }
    }
  },
  methods: {
    checkLoad() {
      const { model: model2, originUrl } = this;
      model2.$loaded = false;
      return Promise$1.try(() => {
        return utils.loadAudio(originUrl, this.options.fitCrossOrigin);
      }).then(() => {
        model2.$loaded = true;
        this.$events.$emit("element.loaded", model2);
      }).catch((ex) => {
        model2.$loaded = true;
        this.$events.$emit("element.loadError", ex, model2);
      });
    }
  },
  mounted() {
    this.audio = this.$refs.audio;
    this.$nextTick(this.checkLoad);
  }
};
var index = {
  model,
  methods,
  layoutComponent: AudioElement
};
export { index as default };
