// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var widget_1 = require('./widget');
var _ = require('underscore');
function serialize_datetime(value, manager) {
    if (value === null) {
        return null;
    }
    else {
        value = new Date(value);
        return {
            year: value.getFullYear(),
            month: value.getMonth(),
            date: value.getDate(),
            hours: value.getHours(),
            minutes: value.getMinutes(),
            seconds: value.getSeconds(),
            milliseconds: value.getMilliseconds()
        };
    }
}
exports.serialize_datetime = serialize_datetime;
;
function deserialize_datetime(value, manager) {
    if (value === null) {
        return null;
    }
    else {
        return new Date(value.year, value.month, value.date, value.hours, value.minutes, value.seconds, value.milliseconds);
    }
}
exports.deserialize_datetime = deserialize_datetime;
;
function createDateAsUTC(date) {
    if (date === null) {
        return null;
    }
    else {
        return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
    }
}
function convertDateToUTC(date) {
    if (date === null) {
        return null;
    }
    else {
        return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
    }
}
var DatePickerModel = (function (_super) {
    __extends(DatePickerModel, _super);
    function DatePickerModel() {
        _super.apply(this, arguments);
    }
    DatePickerModel.prototype.defaults = function () {
        return _.extend(_super.prototype.defaults.call(this), {
            value: null,
            _model_name: 'DatePickerModel',
            _view_name: 'DatePickerView'
        });
    };
    DatePickerModel.serializers = _.extend({
        value: {
            serialize: serialize_datetime,
            deserialize: deserialize_datetime
        }
    }, widget_1.LabeledDOMWidgetModel.serializers);
    return DatePickerModel;
}(widget_1.LabeledDOMWidgetModel));
exports.DatePickerModel = DatePickerModel;
var DatePickerView = (function (_super) {
    __extends(DatePickerView, _super);
    function DatePickerView() {
        _super.apply(this, arguments);
    }
    DatePickerView.prototype.render = function () {
        _super.prototype.render.call(this);
        this.el.classList.add('jupyter-widgets');
        this.el.classList.add('widget-inline-hbox');
        this.el.classList.add('widget-datepicker');
        this._datepicker = document.createElement('input');
        this._datepicker.setAttribute('type', 'date');
        this.el.appendChild(this._datepicker);
        this.listenTo(this.model, 'change:value', this._update_value);
        this._update_value();
    };
    DatePickerView.prototype.events = function () {
        return {
            'change [type="date"]': '_picker_change',
        };
    };
    DatePickerView.prototype._update_value = function () {
        var value = this.model.get('value');
        this._datepicker.valueAsDate = createDateAsUTC(value);
    };
    DatePickerView.prototype._picker_change = function () {
        this.model.set('value', convertDateToUTC(this._datepicker.valueAsDate));
        this.touch();
    };
    return DatePickerView;
}(widget_1.LabeledDOMWidgetView));
exports.DatePickerView = DatePickerView;
