/*! @license
 * Shaka Player
 * Copyright 2016 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

goog.provide('shaka.polyfill.VTTCue');

goog.require('shaka.log');
goog.require('shaka.polyfill');


/**
 * @summary A polyfill to provide VTTCue.
 */
shaka.polyfill.VTTCue = class {
  /**
   * Install the polyfill if needed.
   */
  static install() {
    if (window.VTTCue) {
      shaka.log.info('Using native VTTCue.');
      return;
    }

    if (!window.TextTrackCue) {
      shaka.log.error('VTTCue not available.');
      return;
    }

    const constructorLength = TextTrackCue.length;
    if (constructorLength == 3) {
      shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.');
      window['VTTCue'] = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_;
    } else if (constructorLength == 6) {
      shaka.log.info('Using VTTCue polyfill from 6 argument TextTrackCue.');
      window['VTTCue'] = shaka.polyfill.VTTCue.from6ArgsTextTrackCue_;
    } else if (shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_()) {
      shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.');
      window['VTTCue'] = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_;
    }
  }

  /**
   * Draft spec TextTrackCue with 3 constructor arguments.
   * @see {@link https://bit.ly/2IdyKbA W3C Working Draft 25 October 2012}.
   *
   * @param {number} startTime
   * @param {number} endTime
   * @param {string} text
   * @return {!TextTrackCue}
   * @private
   */
  static from3ArgsTextTrackCue_(startTime, endTime, text) {
    return new window.TextTrackCue(startTime, endTime, text);
  }

  /**
   * Draft spec TextTrackCue with 6 constructor arguments (5th & 6th are
   * optional).
   * @see {@link https://bit.ly/2KaGSP2 W3C Working Draft 29 March 2012}.
   *
   * @param {number} startTime
   * @param {number} endTime
   * @param {string} text
   * @return {!TextTrackCue}
   * @private
   */
  static from6ArgsTextTrackCue_(startTime, endTime, text) {
    const id = startTime + '-' + endTime + '-' + text;
    // Quoting the access to the TextTrackCue object to satisfy the compiler.
    return new window['TextTrackCue'](id, startTime, endTime, text);
  }

  /**
   * IE10, IE11 and Edge return TextTrackCue.length = 0, although they accept 3
   * constructor arguments.
   *
   * @return {boolean}
   * @private
   */
  static canUse3ArgsTextTrackCue_() {
    try {
      return !!shaka.polyfill.VTTCue.from3ArgsTextTrackCue_(1, 2, '');
    } catch (error) {
      return false;
    }
  }
};


shaka.polyfill.register(shaka.polyfill.VTTCue.install);

@@ -537,24 +537,119 @@
 urn;%0A    %7D%0A%0A
+    /** @type %7B?function(number, number, string):!TextTrackCue%7D */%0A    let replacement = null;%0A
     const co
@@ -687,16 +687,16 @@
 length;%0A
-
     if (
@@ -804,32 +804,27 @@
 ;%0A      
-window%5B'VTTCue'%5D
+replacement
  = shaka
@@ -988,32 +988,27 @@
 ;%0A      
-window%5B'VTTCue'%5D
+replacement
  = shaka
@@ -1202,24 +1202,19 @@
     
-window%5B'VTTCue'%5D
+replacement
  = s
@@ -1259,24 +1259,505 @@
 kCue_;%0A    %7D
+%0A%0A    if (!replacement) %7B%0A      shaka.log.error('No recognized signature for TextTrackCue found!');%0A      return;%0A    %7D%0A%0A    // The polyfilled VTTCue must be callable with %22new%22, but the static methods%0A    // in this class cannot be called that way on legacy Edge.  So we must wrap%0A    // the replacement in a plain function.%0A    // eslint-disable-next-line no-restricted-syntax%0A    window%5B'VTTCue'%5D = function(start, end, text) %7B%0A      return replacement(start, end, text);%0A    %7D;
 %0A  %7D%0A%0A  /**%0A

Fix VTTCue polyfill in uncompiled mode on Edge