import Environment from "../util/Environment.js";

const sounds = new Map();

let muted = false;
let loopMuted = false;
let initialized = true;

if (Environment.mobile) {
  initialized = false;

  const onTouchEnd = () => {
    window.removeEventListener("touchend", onTouchEnd);
    for (const sound of sounds.values()) {
      sound._audio.play();
      if (!sound._audio.autoplay) {
        const pauseElement = () => {
          sound._audio.pause();
          sound._audio.removeEventListener("playing", pauseElement);
        };
        sound._audio.addEventListener("playing", pauseElement);
      }
    }
    initialized = true;
  };

  window.addEventListener("touchend", onTouchEnd);
}

export default class Sound {
  static set muted(value) {
    muted = value;
    for (const sound of sounds.values()) {
      sound.muted = sound.muted;
    }
  }

  static get muted() {
    return muted;
  }

  static set loopMuted(value) {
    loopMuted = value;
    for (const sound of sounds.values()) {
      sound.muted = sound.muted;
    }
  }

  static get loopMuted() {
    return loopMuted;
  }

  static add(options) {
    const sound = new Sound(options);
    return sound;
  }

  static get(name) {
    return sounds.get(name);
  }

  constructor({
    src,
    name = src,
    amplification = 1,
    volume = 1,
    loop = false,
    autoplay = false,
  }) {
    this.name = name;

    if (sounds.get(this.name)) {
      throw new Error(`Sound with name/src "${this.name}" already exists, choose another name or use Sound.get(name) to get sound.`);
    }
    sounds.set(this.name, this);

    this._audio = document.createElement("audio");
    this._audio.src = src;

    this.amplification = amplification;
    this.volume = volume;
    this.loop = loop;
    this.autoplay = autoplay;

    this.muted = muted;
  }

  get src() {
    return this._audio.src;
  }

  set src(value) {
    this._audio.src = value;
  }

  get muted() {
    return this._muted;
  }

  set muted(value) {
    this._muted = value;
    this._audio.muted = muted || loopMuted && this.loop ? true : value;
  }

  get paused() {
    return this._audio.paused;
  }

  get loop() {
    return this._audio.loop;
  }

  set loop(value) {
    this.muted = this.muted;
    this._audio.loop = value;
  }

  get currentTime() {
    return this._audio.currentTime;
  }

  set currentTime(value) {
    this._audio.currentTime = value;
  }

  get autoplay() {
    return this._audio.autoplay;
  }

  set autoplay(value) {
    this._audio.autoplay = value;
  }

  get duration() {
    return this._audio.duration;
  }

  get volume() {
    return this._volume;
  }

  set volume(value) {
    this._volume = value;
    this._audio.volume = this._volume * this.amplification;
  }

  play() {
    if (!initialized) {
      return;
    }
    this._audio.play();
  }

  stop() {
    this.currentTime = 0;
    this.pause();
  }

  pause() {
    this._audio.pause();
  }
}

@@ -1,416 +1,103 @@
-im
+ex
 port 
-Environment from %22../util/Environment.js%22;%0A%0Aconst sounds = new Map();%0A%0Alet muted = false;%0Alet loopMuted = false;%0Alet initialized = true;%0A%0Aif (Environment.mobile) %7B%0A  initialized = false;%0A%0A  const onTouchEnd = () =%3E %7B%0A    window.removeEventListener(%22touchend%22, onTouchEnd);%0A    for (const sound of sounds.values()
+default class Sound extends Audio %7B%0A  constructor(%7B src %7D
 ) %7B%0A
-  
     s
-ound._audio.play();%0A      if (!sound._audio.autoplay) %7B%0A   
+uper(src);%0A%0A
     
- 
 const 
-pauseElement
+load
  = (
@@ -113,1471 +113,143 @@
     
-    sound._audio.pause();%0A          sound._audio.removeEventListener(%22playing%22, pauseElement);%0A        %7D;%0A        sound._audio.addEventListener(%22playing%22, pauseElement);%0A      %7D%0A    %7D%0A    initialized = true;%0A  %7D;%0A%0A  window.addEventListener(%22touchend%22, onTouchEnd);%0A%7D%0A%0Aexport default class Sound %7B%0A  static set muted(value) %7B%0A    muted = value;%0A    for (const sound of sounds.values()) %7B%0A      sound.muted = sound.muted;%0A    %7D%0A  %7D%0A%0A  static get muted() %7B%0A    return muted;%0A  %7D%0A%0A  static set loopMuted(value) %7B%0A    loopMuted = value;%0A    for (const sound of sounds.values()) %7B%0A      sound.muted = sound.muted;%0A    %7D%0A  %7D%0A%0A  static get loopMuted() %7B%0A    return loopMuted;%0A  %7D%0A%0A  static add(options) %7B%0A    const sound = new Sound(options);%0A    return sound;%0A  %7D%0A%0A  static get(name) %7B%0A    return sounds.get(name);%0A  %7D%0A%0A  constructor(%7B%0A    src,%0A    name = src,%0A    amplification = 1,%0A    volume = 1,%0A    loop = false,%0A    autoplay = false,%0A  %7D) %7B%0A    this.name = name;%0A%0A    if (sounds.get(this.name)) %7B%0A      throw new Error(%60Sound with name/src %22$%7Bthis.name%7D%22 already exists, choose another name or use Sound.get(name) to get sound.%60);%0A    %7D%0A    sounds.set(this.name, this);%0A%0A    this._audio = document.createElement(%22audio%22);%0A    this._audio.src = src;%0A%0A    this.amplification = amplification;%0A    this.volume = volume;%0A    this.loop = loop;%0A    this.autoplay = autoplay;%0A%0A    this.muted = muted;%0A  %7D%0A%0A  get src() %7B%0A    return this._audio.src;%0A  %7D%0A%0A  set src(value) %7B%0A
+window.removeEventListener(%22click%22, load);%0A      const play = this.autoplay %7C%7C !this.paused;%0A      this.play();%0A      if (!play) %7B%0A    
     
@@ -257,512 +257,75 @@
 his.
-_audio.src = value;%0A  %7D%0A%0A  get muted() %7B%0A    return this._muted;%0A
+pause();%0A    
   %7D%0A
-%0A
   
-set muted(value) %7B%0A    this._muted = value;%0A    this._audio.muted = muted %7C%7C loopMuted && this.loop ? true : value;%0A  %7D%0A%0A  get paused() %7B%0A    return this._audio.paused;%0A  %7D%0A%0A  get loop() %7B%0A    return this._audio.loop;%0A  %7D%0A%0A  set loop(value) %7B%0A    this.muted = this.muted;%0A    this._audio.loop = value;%0A  %7D%0A%0A  get currentTime() %7B%0A    return this._audio.currentTime;%0A  %7D%0A%0A  set currentTime(value) %7B%0A    this._audio.currentTime = value
+  %7D;%0A%0A    window.addEventListener(%22click%22, load)
 ;%0A  
@@ -333,16 +333,8 @@
 %0A%0A  
-get auto
 play
@@ -353,493 +353,72 @@
 urn 
-this._audio.autoplay;%0A  %7D%0A%0A  set autoplay(value) %7B%0A    this._audio.autoplay = value;%0A  %7D%0A%0A  get duration() %7B%0A    return this._audio.duration;%0A  %7D%0A%0A  get volume() %7B%0A    return this._volume;%0A  %7D%0A%0A  set volume(value) %7B%0A    this._volume = value;%0A    this._audio.volume = this._volume * this.amplification;%0A  %7D%0A%0A  play() %7B%0A    if (!initialized) %7B%0A      return;%0A    %7D%0A    this._audio.play();%0A  %7D%0A%0A  stop() %7B%0A    this.currentTime = 0;%0A    this.pause();%0A  %7D%0A%0A  pause() %7B%0A    this._audio.pause(
+super.play().catch((error) =%3E %7B%0A      console.warn(error);%0A    %7D
 );%0A 

simplify Sound.js