import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs2/core-js/object/get-own-property-descriptor";
import _inheritsLoose from "../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import _applyDecoratedDescriptor from "@babel/runtime-corejs2/helpers/applyDecoratedDescriptor";

var _dec, _class, _init, _class2, _temp;

import Intact from 'intact-angular';
import template from './time.vdt';
import dayjs from 'dayjs';
var DatepickerTime = (_dec = Intact.template(), (_class = (_temp = _class2 =
/*#__PURE__*/
function (_Intact) {
  _inheritsLoose(DatepickerTime, _Intact);

  function DatepickerTime() {
    return _Intact.apply(this, arguments) || this;
  }

  var _proto = DatepickerTime.prototype;

  _proto.defaults = function defaults() {
    return {
      data: undefined,
      value: undefined,
      min: undefined,
      max: undefined,
      date: undefined,
      disabledItems: [],
      _value: undefined
    };
  };

  _proto._init = function _init() {
    var _this = this;

    this.on('$receive:value', function (c, v) {
      _this.set('_value', v, {
        silent: true
      });
    });
    this.on('$change:_value', function (c, v) {
      if (!_this._isDisabled(v)) {
        _this._shouldTriggerChange = true;

        _this.set('value', v, {
          async: true
        });

        _this._shouldTriggerChange = false;
      }
    });
    this.on('$change:value', function (c, v) {
      if (_this._shouldTriggerChange) {
        _this.trigger('change', _this, v);
      }
    });
  };

  _proto._onChangeValue = function _onChangeValue(index, c, v) {
    var _value = (this.get('_value') || []).slice(0);

    _value[index] = v;
    this.set('_value', _value);
  };

  _proto._disable = function _disable(index, v) {
    var value = this.get('_value').slice(0);
    value[index] = v;
    return this._isDisabled(value);
  };

  _proto._isDisabled = function _isDisabled(value) {
    if (!value.every(function (item) {
      return item;
    })) return true;

    var _this$get = this.get(),
        min = _this$get.min,
        max = _this$get.max,
        date = _this$get.date;

    value = date.hour(+value[0]).minute(+value[1]).second(+value[2]);
    return min && value.isBefore(min) || max && value.isAfter(max);
  };

  _proto._beforeUpdate = function _beforeUpdate(vNode) {
    // if component shows value on a disabled item,
    // after that the disabled item becomes enabled item,
    // we should set the value to the showed value.
    if (vNode) {
      var _this$get2 = this.get(),
          _value = _this$get2._value;

      if (!this._isDisabled(_value)) {
        this._shouldTriggerChange = true;
        this.set('value', _value, {
          async: true
        });
        this._shouldTriggerChange = false;
      }
    }
  };

  return DatepickerTime;
}(Intact), _defineProperty(_class2, "template", template), _defineProperty(_class2, "propTypes", {
  data: Array,
  value: Array,
  min: dayjs,
  max: dayjs,
  date: {
    required: true,
    type: dayjs
  },
  disabledItems: Array
}), _temp), (_applyDecoratedDescriptor(_class, "template", [_dec], (_init = _Object$getOwnPropertyDescriptor(_class, "template"), _init = _init ? _init.value : undefined, {
  enumerable: true,
  configurable: true,
  writable: true,
  initializer: function initializer() {
    return _init;
  }
}), _class)), _class));
export { DatepickerTime as default };