'use strict';

var RCV = require('@progress/telerik-common-report-viewer');

var stringFormatRegExp = /{(\w+?)}/g;
var specialKeys = {
  DELETE: 46,
  BACKSPACE: 8,
  TAB: 9,
  ESC: 27,
  LEFT: 37,
  UP: 38,
  RIGHT: 39,
  DOWN: 40,
  END: 35,
  HOME: 36
};
function isSpecialKey() {
  var userAgent = window.navigator.userAgent.toLowerCase();
  if (userAgent.indexOf("firefox") > -1) {
    var specialKeysArray = Object.keys(specialKeys);
    var specialKeysLength = specialKeysArray.length;
    return function(keyCode) {
      for (var i = 0; i < specialKeysLength; i++) {
        if (specialKeys[specialKeysArray[i]] == keyCode) {
          return true;
        }
      }
    };
  }
  return function(keyCode) {
    return false;
  };
}
function rectangle(left, top, width, height) {
  return {
    left,
    top,
    width,
    height,
    right: function() {
      return left + width;
    },
    bottom: function() {
      return top + height;
    },
    union: function(other) {
      var newLeft = Math.min(left, other.left);
      var newTop = Math.min(top, other.top);
      var newWidth = Math.max(this.right(), other.right()) - newLeft;
      var newHeight = Math.max(this.bottom(), other.bottom()) - newTop;
      return rectangle(
        newLeft,
        newTop,
        newWidth,
        newHeight
      );
    }
  };
}
function generateGuidString() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    var r = Math.random() * 16 | 0;
    var v = c == "x" ? r : r & 3 | 8;
    return v.toString(16);
  });
}
function trim(s, charlist) {
  return rTrim(lTrim(s, charlist), charlist);
}
function replaceAll(str, find, replace) {
  return str.replace(new RegExp(find, "g"), replace);
}
function lTrim(s, charlist) {
  if (charlist === void 0) {
    charlist = "s";
  }
  return s.replace(new RegExp("^[" + charlist + "]+"), "");
}
function rTrim(s, charlist) {
  if (charlist === void 0) {
    charlist = "s";
  }
  return s.replace(new RegExp("[" + charlist + "]+$"), "");
}
function stringFormat(template, data) {
  var isArray = Array.isArray(data);
  return template.replace(stringFormatRegExp, function($0, $1) {
    return data[isArray ? parseInt($1) : $1];
  });
}
function escapeHtml(str) {
  return $("<div>").text(str).html();
}
function adjustTimezone(date) {
  return new Date(
    Date.UTC(
      date.getFullYear(),
      date.getMonth(),
      date.getDate(),
      date.getHours(),
      date.getMinutes(),
      date.getSeconds(),
      date.getMilliseconds()
    )
  );
}
function unadjustTimezone(date) {
  return new Date(
    date.getUTCFullYear(),
    date.getUTCMonth(),
    date.getUTCDate(),
    date.getUTCHours(),
    date.getUTCMinutes(),
    date.getUTCSeconds(),
    date.getUTCMilliseconds()
  );
}
function areEqual(v1, v2) {
  if (v1 instanceof Date && v2 instanceof Date) {
    if (v1.getTime() !== v2.getTime()) {
      return false;
    }
  } else if (v1 !== v2) {
    return false;
  }
  return true;
}
function areEqualArrays(array1, array2) {
  if (array1 === null) {
    if (array2 !== null) {
      return false;
    }
    return true;
  }
  if (array2 === null) {
    return false;
  }
  if (array1.length !== array2.length) {
    return false;
  }
  for (var j = array1.length - 1; j >= 0; j--) {
    if (!areEqual(array1[j], array2[j])) {
      return false;
    }
  }
  return true;
}
function isApplicationException(xhr) {
  return isApplicationExceptionInstance(getExceptionInstance(xhr));
}
function isApplicationExceptionInstance(exception) {
  var exceptionShortName = "DrawingFactoryUnavailableException";
  return isExceptionInstanceOfType(exception, exceptionShortName, "Telerik.Drawing.Contract." + exceptionShortName);
}
function isExceptionInstanceOfType(exceptionInstance, exceptionTypeShortName, exceptionTypeFullName) {
  return exceptionInstance && exceptionInstance.exceptionType && exceptionTypeNamesMatch(exceptionInstance.exceptionType, exceptionTypeShortName, exceptionTypeFullName);
}
function exceptionTypeNamesMatch(instanceTypeName, exceptionTypeShortName, exceptionTypeFullName) {
  return instanceTypeName && (instanceTypeName === exceptionTypeFullName || instanceTypeName.endsWith(exceptionTypeShortName));
}
function parseJSON(json) {
  try {
    return JSON.parse(
      json,
      function(key, value) {
        if (key && value) {
          var firstChar = key.charAt(0);
          if (firstChar === firstChar.toUpperCase()) {
            var newPropertyName = firstChar.toLowerCase() + key.slice(1);
            this[newPropertyName] = value;
          }
        }
        return value;
      }
    );
  } catch (e) {
    return null;
  }
}
function getExceptionInstance(xhr) {
  if (!xhr || !xhr.responseText) {
    return false;
  }
  return parseJSON(xhr.responseText);
}
function filterUniqueLastOccurrence(array) {
  function onlyLastUnique(value, index, self) {
    return self.lastIndexOf(value) === index;
  }
  return array.filter(onlyLastUnique);
}
function logError(error) {
  var console = window.console;
  if (console && console.error) {
    console.error(error);
  }
}
function toRgbColor(hexColor) {
  if (hexColor && hexColor.length < 6) {
    var index = 1;
    var hexParts = hexColor.split("");
    if (hexParts[0] !== "#") {
      index = 0;
    }
    for (index; index < hexParts.length; index++) {
      hexParts[index] = hexParts[index] + hexParts[index];
    }
    hexColor = hexParts.join("");
  }
  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexColor);
  return result ? parseInt(result[1], 16) + ", " + parseInt(result[2], 16) + ", " + parseInt(result[3], 16) : null;
}
function isRgbColor(color) {
  if (!color) {
    return false;
  }
  return color.indexOf(",") > -1 ? true : false;
}
function getColorAlphaValue(color) {
  if (color.toLowerCase() === "transparent") {
    return 0;
  }
  if (!isRgbColor(color)) {
    return 1;
  }
  if (color.indexOf("#") !== -1) {
    color = toRgbColor(color);
  }
  var colorComponents = color.split(",").map(function(c) {
    return c.trim();
  });
  var alpha = colorComponents.length === 4 ? parseFloat((parseFloat(colorComponents[3].replace(/[()]/g, "")) / 255).toFixed(2)) : 1;
  return alpha;
}

Object.defineProperty(exports, "parseToLocalDate", {
    enumerable: true,
    get: function () { return RCV.parseToLocalDate; }
});
Object.defineProperty(exports, "reportSourcesAreEqual", {
    enumerable: true,
    get: function () { return RCV.reportSourcesAreEqual; }
});
Object.defineProperty(exports, "tryParseFloat", {
    enumerable: true,
    get: function () { return RCV.tryParseFloat; }
});
Object.defineProperty(exports, "tryParseInt", {
    enumerable: true,
    get: function () { return RCV.tryParseInt; }
});
exports.adjustTimezone = adjustTimezone;
exports.areEqual = areEqual;
exports.areEqualArrays = areEqualArrays;
exports.escapeHtml = escapeHtml;
exports.exceptionTypeNamesMatch = exceptionTypeNamesMatch;
exports.filterUniqueLastOccurrence = filterUniqueLastOccurrence;
exports.generateGuidString = generateGuidString;
exports.getColorAlphaValue = getColorAlphaValue;
exports.getExceptionInstance = getExceptionInstance;
exports.isApplicationException = isApplicationException;
exports.isApplicationExceptionInstance = isApplicationExceptionInstance;
exports.isRgbColor = isRgbColor;
exports.isSpecialKey = isSpecialKey;
exports.lTrim = lTrim;
exports.logError = logError;
exports.parseJSON = parseJSON;
exports.rTrim = rTrim;
exports.rectangle = rectangle;
exports.replaceAll = replaceAll;
exports.stringFormat = stringFormat;
exports.toRgbColor = toRgbColor;
exports.trim = trim;
exports.unadjustTimezone = unadjustTimezone;
