"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* @flow */
const React = __importStar(require("react"));
{
    WeekPeriodProps;
}
from;
'./types';
const classnames_1 = __importDefault(require("classnames"));
const prop_types_1 = __importDefault(require("prop-types"));
const withStyles_1 = __importDefault(require("components/hoc/withStyles"));
const ErrorBoundary_1 = __importDefault(require("components/hoc/ErrorBoundary"));
const TriangleArrow_1 = __importDefault(require("components/bosons/TriangleArrow"));
const FieldLabel_1 = __importDefault(require("components/bosons/FieldLabel"));
const moment_1 = __importDefault(require("moment"));
const styles_1 = __importDefault(require("./styles"));
moment_1.default.locale('en-us', {
    week: {
        dow: 1,
    },
});
class WeekPeriod extends React.Component {
    constructor(props) {
        super(props);
        this._updateWeekPeriod = (props, updateType) => {
            const state = this.state;
            let finalYear = updateType === 'year' ? props.year : state.year;
            let finalWeek = updateType === 'week' ? props.week : state.week;
            const startWeekDay = moment_1.default()
                .year(finalYear)
                .week(finalWeek)
                .startOf('week')
                .format();
            const endWeekDay = moment_1.default()
                .year(finalYear)
                .week(finalWeek)
                .endOf('week')
                .format();
            let weekObj = {
                week: finalWeek,
                year: finalYear,
                month: moment_1.default(endWeekDay).month(),
                startWeekDay,
                endWeekDay,
            };
            this.setState(weekObj, () => {
                props.onWeekChange(weekObj);
            });
        };
        this._handleWeekChange = isNext => event => {
            const { year, week } = this.state;
            const { onWeekChange } = this.props;
            const newWeek = moment_1.default()
                .year(year)
                .week(week);
            if (isNext) {
                newWeek.add(1, 'w');
            }
            else {
                newWeek.subtract(1, 'w');
            }
            const startWeekDay = newWeek.startOf('week').format();
            const endWeekDay = newWeek.endOf('week').format();
            const weekObj = {
                week: newWeek.week(),
                year: moment_1.default(endWeekDay).year(),
                month: moment_1.default(endWeekDay).month(),
                startWeekDay,
                endWeekDay,
            };
            this.setState(weekObj, () => {
                onWeekChange(weekObj);
            });
        };
        this.state = {
            week: props.week,
            year: props.year,
            startWeekDay: moment_1.default()
                .year(props.year)
                .week(props.week)
                .startOf('week')
                .format(),
            endWeekDay: moment_1.default()
                .year(props.year)
                .week(props.week)
                .endOf('week')
                .format(),
        };
    }
    componentDidMount() {
        this._updateWeekPeriod(this.props, 'mount');
    }
    componentDidUpdate(prevProps) {
        const props = this.props;
        const state = this.state;
        if (props.week !== prevProps.week && props.week !== state.week) {
            this._updateWeekPeriod(props, 'week');
        }
        if (props.year !== prevProps.year && props.year !== state.year) {
            this._updateWeekPeriod(props, 'year');
        }
    }
    render() {
        const { classes } = this.props;
        const { startWeekDay, endWeekDay } = this.state;
        return (React.createElement(ErrorBoundary_1.default, null,
            React.createElement("div", { className: classes.component },
                React.createElement(FieldLabel_1.default, { label: "Week Period" }),
                React.createElement("div", { className: classes.root },
                    React.createElement("div", { className: classnames_1.default(classes.button, classes.prevButton), onClick: this._handleWeekChange(false) },
                        React.createElement(TriangleArrow_1.default, { direction: 'left', classes: { arrow: classes.arrow } })),
                    React.createElement("div", { className: classes.dateText },
                        moment_1.default(startWeekDay).format('DD MMM YYYY'),
                        " -",
                        ' ',
                        moment_1.default(endWeekDay).format('DD MMM YYYY')),
                    React.createElement("div", { className: classnames_1.default(classes.button, classes.nextButton), onClick: this._handleWeekChange(true) },
                        React.createElement(TriangleArrow_1.default, { direction: 'right', classes: { arrow: classes.arrow } }))))));
    }
}
WeekPeriod.propTypes = {
    week: prop_types_1.default.number,
    year: prop_types_1.default.number,
    onWeekChange: prop_types_1.default.func,
    classes: prop_types_1.default.object,
};
WeekPeriod.defaultProps = {
    week: moment_1.default().week(),
    year: moment_1.default().year(),
    onWeekChange: () => { },
};
exports.default = withStyles_1.default(styles_1.default)(WeekPeriod);
//# sourceMappingURL=index.js.map