text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  var core_1 = require("@angular/core");
  var calendar_service_1 = require("../services/calendar.service");
  var forms_1 = require("@angular/forms");
  var moment = require("moment");
  var config_1 = require("../config");
  exports.ION_CAL_VALUE_ACCESSOR = {
      provide: forms_1.NG_VALUE_ACCESSOR,
      useExisting: core_1.forwardRef(function () { return CalendarComponent; }),
      multi: true
  };
  var CalendarComponent = /** @class */ (function () {
      function CalendarComponent(calSvc) {
          this.calSvc = calSvc;
          this._view = 'days';
          this._calendarMonthValue = [null, null];
          this._showToggleButtons = true;
          this._showMonthPicker = true;
          this.format = config_1.defaults.DATE_FORMAT;
          this.type = 'string';
          this.readonly = false;
          this.onChange = new core_1.EventEmitter();
          this.monthChange = new core_1.EventEmitter();
          this.onSelect = new core_1.EventEmitter();
          this.onSelectStart = new core_1.EventEmitter();
          this.onSelectEnd = new core_1.EventEmitter();
          this._onChanged = function () {
          };
          this._onTouched = function () {
          };
      }
      Object.defineProperty(CalendarComponent.prototype, "showToggleButtons", {
          get: function () {
              return this._showToggleButtons;
          },
          set: function (value) {
              this._showToggleButtons = value;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(CalendarComponent.prototype, "showMonthPicker", {
          get: function () {
              return this._showMonthPicker;
          },
          set: function (value) {
              this._showMonthPicker = value;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(CalendarComponent.prototype, "options", {
          get: function () {
              return this._options;
          },
          set: function (value) {
              this._options = value;
              this.initOpt();
              if (this.monthOpt && this.monthOpt.original) {
                  this.monthOpt = this.createMonth(this.monthOpt.original.time);
              }
          },
          enumerable: true,
          configurable: true
      });
      CalendarComponent.prototype.ngOnInit = function () {
          this.initOpt();
          this.monthOpt = this.createMonth(new Date().getTime());
      };
      CalendarComponent.prototype.getViewDate = function () {
          return this._handleType(this.monthOpt.original.time);
      };
      CalendarComponent.prototype.setViewDate = function (value) {
          this.monthOpt = this.createMonth(this._payloadToTimeNumber(value));
      };
      CalendarComponent.prototype.switchView = function () {
          this._view = this._view === 'days' ? 'month' : 'days';
      };
      CalendarComponent.prototype.prev = function () {
          if (this._view === 'days') {
              this.backMonth();
          }
          else {
              this.prevYear();
          }
      };
      CalendarComponent.prototype.next = function () {
          if (this._view === 'days') {
              this.nextMonth();
          }
          else {
              this.nextYear();
          }
      };
      CalendarComponent.prototype.prevYear = function () {
          if (moment(this.monthOpt.original.time).year() === 1970)
              return;
          var backTime = moment(this.monthOpt.original.time).subtract(1, 'year').valueOf();
          this.monthOpt = this.createMonth(backTime);
      };
      CalendarComponent.prototype.nextYear = function () {
          var nextTime = moment(this.monthOpt.original.time).add(1, 'year').valueOf();
          this.monthOpt = this.createMonth(nextTime);
      };
      CalendarComponent.prototype.nextMonth = function () {
          var nextTime = moment(this.monthOpt.original.time).add(1, 'months').valueOf();
          this.monthChange.emit({
              oldMonth: this.calSvc.multiFormat(this.monthOpt.original.time),
              newMonth: this.calSvc.multiFormat(nextTime)
          });
          this.monthOpt = this.createMonth(nextTime);
      };
      CalendarComponent.prototype.canNext = function () {
          if (!this._d.to || this._view !== 'days')
              return true;
          return this.monthOpt.original.time < moment(this._d.to).valueOf();
      };
      CalendarComponent.prototype.backMonth = function () {
          var backTime = moment(this.monthOpt.original.time).subtract(1, 'months').valueOf();
          this.monthChange.emit({
              oldMonth: this.calSvc.multiFormat(this.monthOpt.original.time),
              newMonth: this.calSvc.multiFormat(backTime)
          });
          this.monthOpt = this.createMonth(backTime);
      };
      CalendarComponent.prototype.canBack = function () {
          if (!this._d.from || this._view !== 'days')
              return true;
          return this.monthOpt.original.time > moment(this._d.from).valueOf();
      };
      CalendarComponent.prototype.monthOnSelect = function (month) {
          this._view = 'days';
          var newMonth = moment(this.monthOpt.original.time).month(month).valueOf();
          this.monthChange.emit({
              oldMonth: this.calSvc.multiFormat(this.monthOpt.original.time),
              newMonth: this.calSvc.multiFormat(newMonth)
          });
          this.monthOpt = this.createMonth(newMonth);
      };
      CalendarComponent.prototype.onChanged = function ($event) {
          switch (this._d.pickMode) {
              case config_1.pickModes.SINGLE:
                  var date = this._handleType($event[0].time);
                  this._onChanged(date);
                  this.onChange.emit(date);
                  break;
              case config_1.pickModes.RANGE:
                  if ($event[0] && $event[1]) {
                      var rangeDate = {
                          from: this._handleType($event[0].time),
                          to: this._handleType($event[1].time)
                      };
                      this._onChanged(rangeDate);
                      this.onChange.emit(rangeDate);
                  }
                  break;
              case config_1.pickModes.MULTI:
                  var dates = [];
                  for (var i = 0; i < $event.length; i++) {
                      if ($event[i] && $event[i].time) {
                          dates.push(this._handleType($event[i].time));
                      }
                  }
                  this._onChanged(dates);
                  this.onChange.emit(dates);
                  break;
              default:
          }
      };
      CalendarComponent.prototype.swipeEvent = function ($event) {
          var isNext = $event.deltaX < 0;
          if (isNext && this.canNext()) {
              this.nextMonth();
          }
          else if (!isNext && this.canBack()) {
              this.backMonth();
          }
      };
      CalendarComponent.prototype._payloadToTimeNumber = function (value) {
          var date;
          if (this.type === 'string') {
              date = moment(value, this.format);
          }
          else {
              date = moment(value);
          }
          return date.valueOf();
      };
      CalendarComponent.prototype._monthFormat = function (date) {
          return moment(date).format(this._d.monthFormat.replace(/y/g, 'Y'));
      };
      CalendarComponent.prototype.initOpt = function () {
          if (this._options && typeof this._options.showToggleButtons === 'boolean') {
              this.showToggleButtons = this._options.showToggleButtons;
          }
          if (this._options && typeof this._options.showMonthPicker === 'boolean') {
              this.showMonthPicker = this._options.showMonthPicker;
              if (this._view !== 'days' && !this.showMonthPicker) {
                  this._view = 'days';
              }
          }
          this._d = this.calSvc.safeOpt(this._options || {});
      };
      CalendarComponent.prototype.createMonth = function (date) {
          return this.calSvc.createMonthsByPeriod(date, 1, this._d)[0];
      };
      CalendarComponent.prototype._createCalendarDay = function (value) {
          return this.calSvc.createCalendarDay(this._payloadToTimeNumber(value), this._d);
      };
      CalendarComponent.prototype._handleType = function (value) {
          var date = moment(value);
          switch (this.type) {
              case 'string':
                  return date.format(this.format);
              case 'js-date':
                  return date.toDate();
              case 'moment':
                  return date;
              case 'time':
                  return date.valueOf();
              case 'object':
                  return date.toObject();
              default:
                  return date;
          }
      };
      CalendarComponent.prototype.writeValue = function (obj) {
          this._writeValue(obj);
          if (obj) {
              if (this._calendarMonthValue[0]) {
                  this.monthOpt = this.createMonth(this._calendarMonthValue[0].time);
              }
              else {
                  this.monthOpt = this.createMonth(new Date().getTime());
              }
          }
      };
      CalendarComponent.prototype.registerOnChange = function (fn) {
          this._onChanged = fn;
      };
      CalendarComponent.prototype.registerOnTouched = function (fn) {
          this._onTouched = fn;
      };
      CalendarComponent.prototype._writeValue = function (value) {
          var _this = this;
          if (!value) {
              this._calendarMonthValue = [null, null];
              return;
          }
          switch (this._d.pickMode) {
              case 'single':
                  this._calendarMonthValue[0] = this._createCalendarDay(value);
                  break;
              case 'range':
                  if (value.from) {
                      this._calendarMonthValue[0] = value.from ? this._createCalendarDay(value.from) : null;
                  }
                  if (value.to) {
                      this._calendarMonthValue[1] = value.to ? this._createCalendarDay(value.to) : null;
                  }
                  break;
              case 'multi':
                  if (Array.isArray(value)) {
                      this._calendarMonthValue = value.map(function (e) {
                          return _this._createCalendarDay(e);
                      });
                  }
                  else {
                      this._calendarMonthValue = [null, null];
                  }
                  break;
              default:
          }
      };
      CalendarComponent.decorators = [
          { type: core_1.Component, args: [{
                      selector: 'ion-calendar',
                      providers: [exports.ION_CAL_VALUE_ACCESSOR],
                      template: "\n    <div class=\"title\">\n      <ng-template [ngIf]=\"_showMonthPicker\" [ngIfElse]=\"title\">\n        <button type=\"button\"\n                ion-button\n                clear\n                class=\"switch-btn\"\n                (click)=\"switchView()\">\n          {{_monthFormat(monthOpt.original.time)}}\n          <ion-icon class=\"arrow-dropdown\"\n                    [name]=\"_view === 'days' ? 'md-arrow-dropdown' : 'md-arrow-dropup'\"></ion-icon>\n        </button>\n      </ng-template>\n      <ng-template #title>\n        <div class=\"switch-btn\">\n          {{_monthFormat(monthOpt.original.time)}}\n        </div>\n      </ng-template>\n      <ng-template [ngIf]=\"_showToggleButtons\">\n        <button type='button' ion-button clear class=\"back\" [disabled]=\"!canBack()\" (click)=\"prev()\">\n          <ion-icon name=\"ios-arrow-back\"></ion-icon>\n        </button>\n        <button type='button' ion-button clear class=\"forward\" [disabled]=\"!canNext()\" (click)=\"next()\">\n          <ion-icon name=\"ios-arrow-forward\"></ion-icon>\n        </button>\n      </ng-template>\n    </div>\n    <ng-template [ngIf]=\"_view === 'days'\" [ngIfElse]=\"monthPicker\">\n      <ion-calendar-week color=\"transparent\"\n                         [weekArray]=\"_d.weekdays\"\n                         [weekStart]=\"_d.weekStart\">\n      </ion-calendar-week>\n\n      <ion-calendar-month class=\"component-mode\"\n                          [(ngModel)]=\"_calendarMonthValue\"\n                          [month]=\"monthOpt\"\n                          [readonly]=\"readonly\"\n                          (onChange)=\"onChanged($event)\"\n                          (swipe)=\"swipeEvent($event)\"\n                          (onSelect)=\"onSelect.emit($event)\"\n                          (onSelectStart)=\"onSelectStart.emit($event)\"\n                          (onSelectEnd)=\"onSelectEnd.emit($event)\"\n                          [pickMode]=\"_d.pickMode\"\n                          [color]=\"_d.color\">\n      </ion-calendar-month>\n    </ng-template>\n\n    <ng-template #monthPicker>\n      <ion-calendar-month-picker [color]=\"_d.color\"\n                                 [monthFormat]=\"_options?.monthPickerFormat\"\n                                 (onSelect)=\"monthOnSelect($event)\"\n                                 [month]=\"monthOpt\">\n      </ion-calendar-month-picker>\n    </ng-template>\n  "
                  },] },
      ];
      /** @nocollapse */
      CalendarComponent.ctorParameters = function () { return [
          { type: calendar_service_1.CalendarService, },
      ]; };
      CalendarComponent.propDecorators = {
          "format": [{ type: core_1.Input },],
          "type": [{ type: core_1.Input },],
          "readonly": [{ type: core_1.Input },],
          "onChange": [{ type: core_1.Output },],
          "monthChange": [{ type: core_1.Output },],
          "onSelect": [{ type: core_1.Output },],
          "onSelectStart": [{ type: core_1.Output },],
          "onSelectEnd": [{ type: core_1.Output },],
          "options": [{ type: core_1.Input },],
      };
      return CalendarComponent;
  }());
  exports.CalendarComponent = CalendarComponent;
  //# sourceMappingURL=calendar.component.js.map