/**
 * Copyright 2015 CANAL+ Group
 *
 * 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.
 */

/**
 * Returns global parameters from a TTML Document
 * TODO Missing parameters.
 * @param {Node} tt - <tt> node
 * @throws Error - Throws if the spacing style is invalid.
 * @returns {Object} params
 * @returns {Number} params.frameRate
 * @returns {Number} params.subFrameRate
 * @returns {Number} params.tickRate
 * @returns {string} params.spaceStyle
 */
export default function getParameters(tt) {
  const parsedFrameRate = tt.getAttribute("ttp:frameRate");
  const parsedSubFrameRate = tt.getAttribute("ttp:subFramRate");
  const parsedTickRate = tt.getAttribute("ttp:tickRate");
  const parsedFrameRateMultiplier = tt.getAttribute("ttp:frameRateMultiplier");
  const parsedSpaceStyle = tt.getAttribute("xml:space");

  if (
    parsedSpaceStyle && parsedSpaceStyle !== "default" &&
    parsedSpaceStyle !== "preserve"
  ) {
    throw new Error("Invalid spacing style");
  }

  const nbFrameRate = Number(parsedFrameRate) || 30;
  const nbSubFrameRate = Number(parsedSubFrameRate) || 1;
  const nbTickRate = Number(parsedTickRate) || 0;

  let tickRate;
  let frameRate;
  const subFrameRate = nbSubFrameRate;
  const spaceStyle = parsedSpaceStyle || "default";

  if (nbTickRate === 0) {
    tickRate = parsedFrameRate ? nbFrameRate * nbSubFrameRate : 1;
  }

  if (parsedFrameRateMultiplier) {
    const multiplierResults = /^(\d+) (\d+)$/g.exec(parsedFrameRateMultiplier);
    if (multiplierResults) {
      const numerator = multiplierResults[1];
      const denominator = multiplierResults[2];
      const multiplierNum = numerator / denominator;
      frameRate = nbFrameRate * multiplierNum;
    }
  }

  return {
    tickRate,
    frameRate,
    subFrameRate,
    spaceStyle,
  };
}

@@ -1654,17 +1654,44 @@
 Rate
-;%0A  let f
+ = nbTickRate;%0A  let frameRate = nbF
 rame
@@ -1725,32 +1725,69 @@
 = nbSubFrameRate
+ != null ? nbSubFrameRate : undefined
 ;%0A  const spaceS
@@ -1789,24 +1789,25 @@
 paceStyle = 
+(
 parsedSpaceS
@@ -1823,16 +1823,17 @@
 default%22
+)
 ;%0A%0A  if 
@@ -2089,24 +2089,31 @@
 numerator = 
+Number(
 multiplierRe
@@ -2120,16 +2120,17 @@
 sults%5B1%5D
+)
 ;%0A      
@@ -2149,16 +2149,23 @@
 nator = 
+Number(
 multipli
@@ -2176,16 +2176,17 @@
 sults%5B2%5D
+)
 ;%0A      

fix getParameters