Liferay.Loader.define("portal-workflow-metrics-web@2.0.46/js/components/process-metrics/completion-velocity/util/chart.es", ['module', 'exports', 'require', '../../../../shared/util/moment.es', './chartConstants.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.getXAxisIntervals = exports.getAxisMeasuresFromData = exports.getAxisMeasures = exports.formatYearDate = exports.formatXAxisDate = exports.getRangeKey = exports.formatWeekDateWithYear = exports.formatWeekDate = exports.formatMonthDate = void 0;

    var _moment = _interopRequireDefault(require("../../../../shared/util/moment.es"));

    var _chartConstants = require("./chartConstants.es");

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    function _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

    function _nonIterableSpread() {
      throw new TypeError("Invalid attempt to spread non-iterable instance");
    }

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

    var formatMonthDate = function formatMonthDate(date, timeRange) {
      var currentDate = _moment.default.utc(date);

      var dateEnd = _moment.default.utc(timeRange.dateEnd);

      var dateStart = _moment.default.utc(timeRange.dateStart);

      var firstDayOfMonth = currentDate.clone().startOf('month');
      var lastDayOfMonth = currentDate.clone().endOf('month');

      if (currentDate.isSame(dateStart, 'month')) {
        firstDayOfMonth = currentDate.clone();
      } else if (currentDate.isSame(dateEnd, 'month')) {
        lastDayOfMonth = dateEnd.clone();
      }

      if (firstDayOfMonth.isSame(lastDayOfMonth, 'day')) {
        return firstDayOfMonth.format(Liferay.Language.get('mmm-dd-yyyy'));
      }

      return "".concat(firstDayOfMonth.format(Liferay.Language.get('mmm-dd')), "-").concat(lastDayOfMonth.format(Liferay.Language.get('dd-yyyy')));
    };

    exports.formatMonthDate = formatMonthDate;

    var formatWeekDate = function formatWeekDate(date, timeRange) {
      var currentDate = _moment.default.utc(date);

      var dateEnd = _moment.default.utc(timeRange.dateEnd);

      var dateStart = _moment.default.utc(timeRange.dateStart);

      var firstDayOfWeek = currentDate.clone().startOf('week');
      var lastDayOfWeek = currentDate.clone().endOf('week');

      if (currentDate.isSame(dateStart, 'week')) {
        firstDayOfWeek = currentDate.clone();
      } else if (currentDate.isSame(dateEnd, 'week')) {
        lastDayOfWeek = dateEnd.clone();
      }

      var firstMonth = firstDayOfWeek.format('MMM');
      var lastMonth = lastDayOfWeek.format('MMM');

      if (firstDayOfWeek.isSame(lastDayOfWeek, 'day')) {
        return firstDayOfWeek.format(Liferay.Language.get('mmm-dd'));
      } else if (firstMonth === lastMonth) {
        return "".concat(firstDayOfWeek.format(Liferay.Language.get('mmm-dd')), "-").concat(lastDayOfWeek.format('DD'));
      }

      return "".concat(firstDayOfWeek.format(Liferay.Language.get('mmm-dd')), "-").concat(lastDayOfWeek.format(Liferay.Language.get('mmm-dd')));
    };

    exports.formatWeekDate = formatWeekDate;

    var formatWeekDateWithYear = function formatWeekDateWithYear(date, timeRange) {
      var currentDate = _moment.default.utc(date);

      var dateEnd = _moment.default.utc(timeRange.dateEnd);

      var dateStart = _moment.default.utc(timeRange.dateStart);

      var firstDayOfWeek = currentDate.clone().startOf('week');
      var lastDayOfWeek = currentDate.clone().endOf('week');

      if (currentDate.isSame(dateStart, 'week')) {
        firstDayOfWeek = currentDate.clone();
      } else if (currentDate.isSame(dateEnd, 'week')) {
        lastDayOfWeek = dateEnd.clone();
      }

      var firstMonth = firstDayOfWeek.format('MMM');
      var lastMonth = lastDayOfWeek.format('MMM');
      var firstYear = firstDayOfWeek.format('YYYY');
      var lastYear = lastDayOfWeek.format('YYYY');

      if (firstDayOfWeek.isSame(lastDayOfWeek, 'day')) {
        return firstDayOfWeek.format(Liferay.Language.get('mmm-dd-yyyy'));
      } else if (firstYear !== lastYear) {
        return "".concat(firstDayOfWeek.format(Liferay.Language.get('mmm-dd-yyyy')), " - ").concat(lastDayOfWeek.format(Liferay.Language.get('mmm-dd-yyyy')));
      } else if (firstMonth !== lastMonth) {
        return "".concat(firstDayOfWeek.format(Liferay.Language.get('mmm-dd')), " - ").concat(lastDayOfWeek.format(Liferay.Language.get('mmm-dd-yyyy')));
      }

      return "".concat(firstDayOfWeek.format(Liferay.Language.get('mmm-dd')), " - ").concat(lastDayOfWeek.format(Liferay.Language.get('dd-yyyy')));
    };

    exports.formatWeekDateWithYear = formatWeekDateWithYear;

    var getRangeKey = function getRangeKey(timeRange) {
      var endDate = _moment.default.utc(timeRange.dateEnd);

      var startDate = _moment.default.utc(timeRange.dateStart);

      var diff = parseInt(_moment.default.duration(endDate.diff(startDate)).asDays());
      var diffList = [_chartConstants.TODAY, _chartConstants.YESTERDAY, _chartConstants.LAST_7_DAYS, _chartConstants.LAST_30_DAYS, _chartConstants.LAST_90_DAYS, _chartConstants.LAST_180_DAYS, _chartConstants.LAST_YEAR];
      var diffItem = diffList.find(function (key) {
        return key >= diff;
      });

      if (typeof diffItem !== 'undefined') {
        return diffItem;
      }

      return _chartConstants.LAST_YEAR;
    };

    exports.getRangeKey = getRangeKey;

    var formatXAxisDate = function formatXAxisDate(date, isAmPm, timeRangeKey, timeRange) {
      var currentDate = _moment.default.utc(date);

      var rangeUnit = getRangeKey(timeRange);

      if (timeRangeKey === _chartConstants.HOURS) {
        var datetPattern = Liferay.Language.get('hh-mm');

        if (isAmPm) {
          datetPattern = Liferay.Language.get('hh-mm-a');
        }

        return currentDate.format(datetPattern);
      } else if (timeRangeKey === _chartConstants.YEARS) {
        return currentDate.format('YYYY');
      } else if ([_chartConstants.LAST_YEAR, _chartConstants.LAST_180_DAYS].includes(rangeUnit) && _chartConstants.MONTHS === timeRangeKey) {
        return currentDate.format(Liferay.Language.get('mmm-yyyy'));
      } else if (timeRangeKey === _chartConstants.MONTHS) {
        return currentDate.format('MMM');
      } else if (timeRangeKey === _chartConstants.WEEKS) {
        return formatWeekDate(date, timeRange);
      }

      return currentDate.format(Liferay.Language.get('mmm-dd'));
    };

    exports.formatXAxisDate = formatXAxisDate;

    var formatYearDate = function formatYearDate(date, timeRange) {
      var currentDate = _moment.default.utc(date);

      var dateEnd = _moment.default.utc(timeRange.dateEnd);

      var dateStart = _moment.default.utc(timeRange.dateStart);

      var firstDayOfYear = currentDate.clone().startOf('year');
      var lastDayOfYear = currentDate.clone().endOf('year');

      if (currentDate.isSame(dateStart, 'year')) {
        firstDayOfYear = currentDate.clone();
      } else if (currentDate.isSame(dateEnd, 'year')) {
        lastDayOfYear = dateEnd.clone();
      }

      if (firstDayOfYear.isSame(lastDayOfYear, 'day')) {
        return firstDayOfYear.format(Liferay.Language.get('mmm-dd-yyyy'));
      }

      return "".concat(firstDayOfYear.format(Liferay.Language.get('mmm-dd')), "-").concat(lastDayOfYear.format(Liferay.Language.get('mmm-dd-yyyy')));
    };

    exports.formatYearDate = formatYearDate;

    var getAxisMeasures = function getAxisMeasures(value) {
      var numChars = Math.floor(value).toString().length;
      var decOrder = Math.pow(10, numChars - 1);
      var maxValue = decOrder * Math.floor(value / decOrder) + decOrder;
      var firstDic = maxValue / decOrder;

      if ([3, 7, 9].indexOf(firstDic) > -1) {
        firstDic += 1;
      }

      maxValue = firstDic * decOrder;
      var intervalCount = 4;

      if ([1, 5, 10].indexOf(firstDic) > -1) {
        intervalCount = 5;
      }

      var intervalValue = maxValue / intervalCount;

      for (var i = 0; i < intervalCount; i++) {
        var tempMaxValue = intervalValue * (i + 1);

        if (tempMaxValue > value) {
          maxValue = tempMaxValue;
          intervalCount = i + 1;
          break;
        }
      }

      var intervals = [];
      intervals.push(0);

      for (var _i = 0; _i < intervalCount; _i++) {
        intervals.push(intervalValue * (_i + 1));
      }

      return {
        intervalCount: intervalCount,
        intervalValue: intervalValue,
        intervals: intervals,
        maxValue: maxValue
      };
    };

    exports.getAxisMeasures = getAxisMeasures;

    var getAxisMeasuresFromData = function getAxisMeasuresFromData(data) {
      return getAxisMeasures(Math.max.apply(Math, _toConsumableArray(data.reduce(function (prev, next) {
        return prev.concat(next);
      }, []).filter(function (value) {
        return typeof value === 'number';
      }))));
    };

    exports.getAxisMeasuresFromData = getAxisMeasuresFromData;

    var getXAxisIntervals = function getXAxisIntervals(timeRange, keys, type) {
      var _diffMap;

      var endDate = _moment.default.utc(timeRange.dateEnd);

      var lengthKeys = keys.length;

      var secondDate = _moment.default.utc(keys[1]);

      var startDate = _moment.default.utc(timeRange.dateStart);

      var diffLeftDays = parseInt(_moment.default.duration(secondDate.diff(startDate)).asDays());
      var diffLeftMonths = parseInt(_moment.default.duration(secondDate.diff(startDate)).asMonths());

      var nextToLastDay = _moment.default.utc(keys[lengthKeys - 2]);

      var diffRightDays = parseInt(_moment.default.duration(endDate.diff(nextToLastDay)).asDays());
      var diffMap = (_diffMap = {}, _defineProperty(_diffMap, _chartConstants.TODAY, function () {
        return {
          offset: 4,
          padLeft: 0,
          padRight: 0
        };
      }), _defineProperty(_diffMap, _chartConstants.YESTERDAY, function () {
        return {
          offset: 6,
          padLeft: 0,
          padRight: 0
        };
      }), _defineProperty(_diffMap, _chartConstants.LAST_7_DAYS, function () {
        return {
          offset: 1,
          padLeft: 0,
          padRight: 0
        };
      }), _defineProperty(_diffMap, _chartConstants.LAST_30_DAYS, function (type) {
        if (type === _chartConstants.DAYS) {
          return {
            offset: 6,
            padLeft: 0,
            padRight: diffRightDays < 2 ? diffRightDays + 2 : 0
          };
        }

        return {
          offset: 1,
          padLeft: 0,
          padRight: 0
        };
      }), _defineProperty(_diffMap, _chartConstants.LAST_90_DAYS, function (type) {
        if (type === _chartConstants.DAYS) {
          return {
            offset: 11,
            padLeft: 0,
            padRight: 3
          };
        } else if (type === _chartConstants.WEEKS) {
          var pad = diffLeftDays < 7 ? 3 : 0;
          return {
            offset: 2,
            padLeft: pad,
            padRight: pad
          };
        }

        return {
          offset: 1,
          padLeft: 0,
          padRight: 0
        };
      }), _defineProperty(_diffMap, _chartConstants.LAST_180_DAYS, function (type) {
        if (type === _chartConstants.WEEKS) {
          return {
            offset: 4,
            padLeft: 0,
            padRight: 3
          };
        }

        return {
          offset: 1,
          padLeft: diffLeftDays < 14 ? 1 : 0,
          padRight: 0
        };
      }), _defineProperty(_diffMap, _chartConstants.LAST_YEAR, function (type) {
        if (type === _chartConstants.WEEKS) {
          var lengthWeek = lengthKeys === 52 ? 5 : 6;
          return {
            offset: parseInt(lengthKeys / lengthWeek),
            padLeft: 0,
            padRight: 6
          };
        } else if (type === _chartConstants.YEARS) {
          return {
            offset: lengthKeys > 12 ? parseInt(lengthKeys / 6) : 1,
            padLeft: lengthKeys > 12 && diffLeftMonths < 7 || diffLeftMonths < 2 ? 1 : 0,
            padRight: 0
          };
        }

        return {
          offset: parseInt(lengthKeys / 5),
          padLeft: 0,
          padRight: 0
        };
      }), _diffMap);
      var diffMapKey = getRangeKey(timeRange);
      var diffIndex = diffMap[diffMapKey](type);
      return keys.filter(function (key, index) {
        return index === 0 || index === lengthKeys - 1 || index % diffIndex.offset === 0 && index > diffIndex.padLeft && index < lengthKeys - diffIndex.padRight + 1;
      });
    };

    exports.getXAxisIntervals = getXAxisIntervals;
    //# sourceMappingURL=chart.es.js.map
  }
});
//# sourceMappingURL=chart.es.js.map