import "core-js/modules/es6.array.find";
import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";
import "core-js/modules/es6.regexp.split";
import Button from '../button';
import ScrollSelect from '../scrollSelect';
import Time from './time';
import { Row, Col } from '../grid';
import { strPad, _$ } from '../utils';
import { isEqual, isLT, isGT, getDateString, getTimeString, createDate, yearArray, monthArray } from './utils';
import { Tabs, Tab } from '../tabs';
import Icon from '../icon';
import dayjs from 'dayjs';
export default function (obj, _Vdt, blocks, $callee) {
  _Vdt || (_Vdt = Vdt);
  obj || (obj = {});
  blocks || (blocks = {});
  var h = _Vdt.miss.h,
      hc = _Vdt.miss.hc,
      hu = _Vdt.miss.hu,
      widgets = this && this.widgets || {},
      _blocks = {},
      __blocks = {},
      __u = _Vdt.utils,
      extend = __u.extend,
      _e = __u.error,
      _className = __u.className,
      __slice = __u.slice,
      __noop = __u.noop,
      __m = __u.map,
      __o = __u.Options,
      _getModel = __o.getModel,
      _setModel = __o.setModel,
      _setCheckboxModel = __u.setCheckboxModel,
      _detectCheckboxChecked = __u.detectCheckboxChecked,
      _setSelectModel = __u.setSelectModel,
      self = this.data,
      $this = this,
      scope = obj,
      Animate = self && self.Animate,
      parent = ($callee || {})._super;

  var _self$get = self.get(),
      value = _self$get.value,
      dayClassNames = _self$get.dayClassNames,
      disabledDate = _self$get.disabledDate,
      multiple = _self$get.multiple,
      onMouseEnterDay = _self$get.onMouseEnterDay,
      type = _self$get.type,
      hours = _self$get.hours,
      minutes = _self$get.minutes,
      seconds = _self$get.seconds,
      disabledHours = _self$get.disabledHours,
      disabledMinutes = _self$get.disabledMinutes,
      disabledSeconds = _self$get.disabledSeconds,
      _isShowYearPicker = _self$get._isShowYearPicker,
      _isSelectTime = _self$get._isSelectTime,
      _now = _self$get._now,
      _showDate = _self$get._showDate,
      _focusDate = _self$get._focusDate,
      _id = _self$get._id;

  var values = value ? multiple ? value : [value] : [];
  var valueDate = values[self._index] || _now;
  var showDate = _showDate || valueDate;

  var yearMonthFormat = _$('yearMonthFormat');

  if (yearMonthFormat === 'yearMonthFormat') {
    yearMonthFormat = 'YYYY MM';
  }

  var format = yearMonthFormat.split(' ');
  var map = {
    'MM': h('span', null, function () {
      try {
        return _$(showDate.get('month') + 1 + "\u6708");
      } catch (e) {
        _e(e);
      }
    }.call($this), 'k-text'),
    'YYYY': h('span', null, function () {
      try {
        return _$("{n}\u5E74", {
          n: showDate.get('year')
        });
      } catch (e) {
        _e(e);
      }
    }.call($this), 'k-text')
  };
  var panel = type === 'year' ? map.YYYY : format.map(function (item) {
    return map[item];
  });

  var isYearOrMonth = self._isYearOrMonth();

  return h('div', {
    'tabindex': '0',
    'ev-keydown': function () {
      try {
        return self._onKeydown;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }, [(_blocks['title'] = function (parent) {
    return null;
  }) && (__blocks['title'] = function (parent) {
    var args = arguments;
    return blocks['title'] ? blocks['title'].apply($this, [function () {
      return _blocks['title'].apply($this, args);
    }].concat(__slice.call(args, 1))) : _blocks['title'].apply($this, args);
  }) && __blocks['title'].apply($this, [__noop]), function () {
    try {
      return type === 'datetime';
    } catch (e) {
      _e(e);
    }
  }.call($this) ? h(Tabs, {
    'type': 'border-card',
    'value': function () {
      try {
        return _isSelectTime ? "time" : "date";
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'children': [h(Tab, {
      'value': 'date',
      'children': function () {
        try {
          return values[self._index] ? getDateString(valueDate.toDate()) : '0000-00-00';
        } catch (e) {
          _e(e);
        }
      }.call($this),
      '_context': $this
    }), h(Tab, {
      'value': 'time',
      'children': function () {
        try {
          return values[self._index] ? getTimeString(valueDate.toDate()) : '00:00:00';
        } catch (e) {
          _e(e);
        }
      }.call($this),
      '_context': $this
    })],
    '_context': $this,
    'ev-$change:value': function () {
      try {
        return self._onChangeTab;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }) : undefined, function () {
    try {
      return !_isSelectTime;
    } catch (e) {
      _e(e);
    }
  }.call($this) ? h('div', null, [h('div', null, [h('div', null, [h(Button, {
    'icon': function () {
      try {
        return true;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'type': 'none',
    'size': 'small',
    'className': 'k-prev',
    'tagName': 'div',
    'tabindex': function () {
      try {
        return null;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'children': [h(Icon, {
      'className': 'ion-ios-arrow-left',
      'size': 'small',
      '_context': $this
    }), h(Icon, {
      'className': 'ion-ios-arrow-left',
      'size': 'small',
      '_context': $this
    })],
    '_context': $this,
    'ev-click': function () {
      try {
        return self.prevYear;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }), function () {
    try {
      return type !== 'year';
    } catch (e) {
      _e(e);
    }
  }.call($this) ? h(Button, {
    'icon': function () {
      try {
        return true;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'type': 'none',
    'size': 'small',
    'className': 'k-prev',
    'tagName': 'div',
    'tabindex': function () {
      try {
        return null;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'children': h(Icon, {
      'className': 'ion-ios-arrow-left',
      'size': 'small',
      '_context': $this
    }),
    '_context': $this,
    'ev-click': function () {
      try {
        return self.prevMonth;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }) : undefined], 'k-left-btns'), h('div', null, [h(Button, {
    'icon': function () {
      try {
        return true;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'type': 'none',
    'size': 'small',
    'className': 'k-next',
    'tagName': 'div',
    'tabindex': function () {
      try {
        return null;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'children': [h(Icon, {
      'className': 'ion-ios-arrow-right',
      'size': 'small',
      '_context': $this
    }), h(Icon, {
      'className': 'ion-ios-arrow-right',
      'size': 'small',
      '_context': $this
    })],
    '_context': $this,
    'ev-click': function () {
      try {
        return self.nextYear;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }), function () {
    try {
      return type !== 'year';
    } catch (e) {
      _e(e);
    }
  }.call($this) ? h(Button, {
    'icon': function () {
      try {
        return true;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'type': 'none',
    'size': 'small',
    'className': 'k-next',
    'tagName': 'div',
    'tabindex': function () {
      try {
        return null;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'children': h(Icon, {
      'className': 'ion-ios-arrow-right',
      'size': 'small',
      '_context': $this
    }),
    '_context': $this,
    'ev-click': function () {
      try {
        return self.nextMonth;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }) : undefined], 'k-right-btns'), h('div', {
    'ev-click': function () {
      try {
        return isYearOrMonth ? undefined : self.showYearPicker;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }, [h(Animate, {
    'a:show': function () {
      try {
        return !isYearOrMonth && _isShowYearPicker;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'a:transition': 'c-fade',
    'a:tag': function () {
      try {
        return Icon;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'className': 'ion-ios-undo',
    'key': 'undo',
    '_context': $this
  }), function () {
    try {
      return panel;
    } catch (e) {
      _e(e);
    }
  }.call($this)], 'k-text-wrapper')], 'k-month c-clearfix'), function () {
    try {
      return !_isShowYearPicker;
    } catch (e) {
      _e(e);
    }
  }.call($this) ? h('div', null, __m(function () {
    try {
      return [_$('日'), _$('一'), _$('二'), _$('三'), _$('四'), _$('五'), _$('六')];
    } catch (e) {
      _e(e);
    }
  }.call($this), function (value, key) {
    return h('div', null, function () {
      try {
        return value;
      } catch (e) {
        _e(e);
      }
    }.call($this), 'k-weekday');
  }, $this), 'k-weekdays') : undefined, function () {
    try {
      return !_isShowYearPicker;
    } catch (e) {
      _e(e);
    }
  }.call($this) ? h('div', {
    'ev-mouseleave': function () {
      try {
        return self._onMouseLeaveDays;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }, function () {
    try {
      return function () {
        var ret = []; // dayjs is immutable, so we use js Date

        var start = showDate.toDate();
        var month = start.getMonth();
        start.setDate(1);
        start.setHours(0);
        start.setMinutes(0);
        start.setSeconds(0);
        var startWeekday = start.getDay();
        start.setDate(1 - startWeekday);

        var _loop = function _loop(i) {
          var dayjsDate = dayjs(start);

          var _date = start.getDate();

          var _month = start.getMonth();

          var disabled = self._isDisabledDate(dayjsDate);

          var isOut = _month !== month;
          var classNames = dayClassNames ? dayClassNames.call(self, dayjsDate, isOut) : null;
          ret.push(h('div', {
            'ev-click': function () {
              try {
                return !disabled && self.select.bind(self, dayjsDate);
              } catch (e) {
                _e(e);
              }
            }.call($this),
            'ev-mouseenter': function () {
              try {
                return self._onMouseEnter.bind(self, dayjsDate, isOut);
              } catch (e) {
                _e(e);
              }
            }.call($this)
          }, function () {
            try {
              return _date;
            } catch (e) {
              _e(e);
            }
          }.call($this), _className(function () {
            try {
              return _Object$assign({
                "k-day": true,
                "k-out": isOut,
                "k-active": !isOut && values.find(function (value) {
                  return isEqual(value, dayjsDate);
                }),
                "k-today": isEqual(_now, dayjsDate),
                "k-disabled": disabled,
                "k-hover": isEqual(_focusDate, dayjsDate)
              }, classNames);
            } catch (e) {
              _e(e);
            }
          }.call($this))));
          start.setDate(start.getDate() + 1);
        };

        for (var i = 0; i < 42; i++) {
          _loop(i);
        }

        var result = [];

        var _loop2 = function _loop2(i) {
          result.push(h('div', null, function () {
            try {
              return ret.slice(7 * i, 7 * (i + 1));
            } catch (e) {
              _e(e);
            }
          }.call($this)));
        };

        for (var i = 0; i < 6; i++) {
          _loop2(i);
        }

        return result;
      }();
    } catch (e) {
      _e(e);
    }
  }.call($this), 'k-calendar') : undefined, function () {
    try {
      return _isShowYearPicker;
    } catch (e) {
      _e(e);
    }
  }.call($this) ? h(Row, {
    'className': 'k-year-picker',
    'children': function () {
      try {
        var span, _map;

        return span = type === 'year' ? 24 : 12, _map = {
          MM: h(Col, {
            'span': function () {
              try {
                return span;
              } catch (e) {
                _e(e);
              }
            }.call($this),
            'children': h(ScrollSelect, {
              'data': function () {
                try {
                  return monthArray;
                } catch (e) {
                  _e(e);
                }
              }.call($this),
              'value': function () {
                try {
                  return showDate.month();
                } catch (e) {
                  _e(e);
                }
              }.call($this),
              '_context': $this,
              'ev-$change:value': function () {
                try {
                  return self.onChangeMonth;
                } catch (e) {
                  _e(e);
                }
              }.call($this)
            }),
            '_context': $this
          }),
          YYYY: h(Col, {
            'span': function () {
              try {
                return span;
              } catch (e) {
                _e(e);
              }
            }.call($this),
            'children': h(ScrollSelect, {
              'data': function () {
                try {
                  return yearArray;
                } catch (e) {
                  _e(e);
                }
              }.call($this),
              'value': function () {
                try {
                  return showDate.year();
                } catch (e) {
                  _e(e);
                }
              }.call($this),
              '_context': $this,
              'ev-$change:value': function () {
                try {
                  return self.onChangeYear;
                } catch (e) {
                  _e(e);
                }
              }.call($this)
            }),
            '_context': $this
          })
        }, type === 'year' ? _map.YYYY : format.map(function (item) {
          return _map[item];
        });
      } catch (e) {
        _e(e);
      }
    }.call($this),
    '_context': $this
  }) : undefined], 'k-date-picker') : h('div', null, (_blocks['time-body'] = function (parent) {
    return h(Time, {
      'data': function () {
        try {
          return [hours.map(function (v) {
            return strPad(v, 2);
          }), minutes.map(function (v) {
            return strPad(v, 2);
          }), seconds.map(function (v) {
            return strPad(v, 2);
          })];
        } catch (e) {
          _e(e);
        }
      }.call($this),
      'value': function () {
        try {
          return values[self._index] || // if exists _id, it indicate that this is a range time selection of Timepicker
          // we don't let Time to trigger $change:value in initial
          // otherwise the start time will be disabled due to the end time is 00:00:00
          // #119
          self.get('_id') ? [strPad(valueDate.hour(), 2), strPad(valueDate.minute(), 2), strPad(valueDate.second(), 2)] : ['', '', ''];
        } catch (e) {
          _e(e);
        }
      }.call($this),
      'disabledItems': function () {
        try {
          return [disabledHours, disabledMinutes, disabledSeconds];
        } catch (e) {
          _e(e);
        }
      }.call($this),
      'min': function () {
        try {
          return self._getMinDate();
        } catch (e) {
          _e(e);
        }
      }.call($this),
      'max': function () {
        try {
          return self._getMaxDate();
        } catch (e) {
          _e(e);
        }
      }.call($this),
      'date': function () {
        try {
          return scope.date || valueDate;
        } catch (e) {
          _e(e);
        }
      }.call($this),
      '_context': $this,
      '_blocks': function (blocks) {
        var _blocks = {},
            __blocks = extend({}, blocks);

        return (_blocks['append'] = function (parent) {
          return ':';
        }) && (__blocks['append'] = function (parent) {
          var args = arguments;
          return blocks['append'] ? blocks['append'].apply($this, [function () {
            return _blocks['append'].apply($this, args);
          }].concat(__slice.call(args, 1))) : _blocks['append'].apply($this, args);
        }), __blocks;
      }.call($this, {}),
      'ev-change': function () {
        try {
          return self.onChangeTime;
        } catch (e) {
          _e(e);
        }
      }.call($this)
    });
  }) && (__blocks['time-body'] = function (parent) {
    var args = arguments;
    return blocks['time-body'] ? blocks['time-body'].apply($this, [function () {
      return _blocks['time-body'].apply($this, args);
    }].concat(__slice.call(args, 1))) : _blocks['time-body'].apply($this, args);
  }) && __blocks['time-body'].apply($this, [__noop]), 'k-time-picker', 'time')], _className(function () {
    try {
      return {
        "k-calendar-wrapper": true,
        "k-datetime": type === 'datetime'
      };
    } catch (e) {
      _e(e);
    }
  }.call($this)));
}