/**
* Copyright 2012-2020, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var isNumeric = require('fast-isnumeric');
var ms2DateTime = require('../../lib').ms2DateTime;
var ONEDAY = require('../../constants/numerical').ONEDAY;

var M = {};
for(var n = 1; n <= 12; n++) {
    M['M' + n] = n;
}

module.exports = function alignPeriod(trace, ax, axLetter, vals) {
    var alignment = trace[axLetter + 'periodalignment'];
    if(!alignment || alignment === 'start') return vals;

    var dynamic = false;
    var period = trace[axLetter + 'period'];
    if(isNumeric(period)) {
        period = +period; // milliseconds
    } else if(typeof period === 'string') {
        period = M[period]; // months
        dynamic = true;
    }

    if(period > 0) {
        var ratio = (alignment === 'end') ? 1 : 0.5;

        var len = vals.length;
        for(var i = 0; i < len; i++) {
            var delta;

            if(dynamic) {
                var dateStr = ms2DateTime(vals[i], 0, ax.calendar);
                var d = new Date(dateStr);
                var year = d.getFullYear();
                var month = d.getMonth();

                var totalDaysInMonths = 0;
                for(var k = 0; k < period; k++) {
                    month += 1;
                    if(month > 12) {
                        month = 1;
                        year++;
                    }

                    var monthDays = (
                        new Date(year, month, 0)
                    ).getDate();

                    totalDaysInMonths += monthDays;
                }

                delta = ONEDAY * totalDaysInMonths; // convert to ms
            } else {
                delta = period;
            }

            vals[i] += ratio * delta;
        }
    }
    return vals;
};

@@ -359,74 +359,8 @@
 Y;%0A%0A
-var M = %7B%7D;%0Afor(var n = 1; n %3C= 12; n++) %7B%0A    M%5B'M' + n%5D = n;%0A%7D%0A%0A
 modu
@@ -721,49 +721,186 @@
 ing'
-) %7B%0A        period = M%5B
+ && period.charAt(0) === 'M') %7B%0A        var v = +(period.substring(1));%0A        if(v %3E 0 && Math.round(v) === v) 
 period
-%5D
+ = v
 ; // 
-months
+positive integer months%0A        else return vals;%0A
 %0A   

support positive integers for months