import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs2/core-js/object/get-own-property-descriptor";
import "core-js/modules/es6.number.constructor";
import "core-js/modules/es6.array.find";
import _inheritsLoose from "../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import _applyDecoratedDescriptor from "@babel/runtime-corejs2/helpers/applyDecoratedDescriptor";
import "core-js/modules/es6.regexp.match";

var _dec, _class, _init, _class2, _temp;

import Intact from 'intact-angular';
import template from './index.vdt';
import '../../styles/kpc.styl';
import './index.styl';
var Split = (_dec = Intact.template(), (_class = (_temp = _class2 =
/*#__PURE__*/
function (_Intact) {
  _inheritsLoose(Split, _Intact);

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

  var _proto = Split.prototype;

  _proto.defaults = function defaults() {
    return {
      mode: 'horizontal',
      firstSize: 'auto',
      lastSize: 'auto',
      min: 0,
      max: '100%-6',
      _resizing: false
    };
  };

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

    var fixSize = function fixSize() {
      var _this$get = _this.get(),
          firstSize = _this$get.firstSize,
          lastSize = _this$get.lastSize;

      if (firstSize === 'auto' && lastSize === 'auto') {
        _this.set('firstSize', '50%');
      } else if (firstSize !== 'auto' && lastSize !== 'auto') {
        _this.set('lastSize', 'auto');
      }
    };

    this.on('$receive', function (c, keys) {
      if (['firstSize', 'lastSize'].find(function (key) {
        return ~keys.indexOf(key);
      })) {
        fixSize();
      }
    }); // maybe there aren't props

    fixSize();
  };

  _proto._generateMinMaxValue = function _generateMinMaxValue() {
    var _this$get2 = this.get(),
        firstSize = _this$get2.firstSize,
        lastSize = _this$get2.lastSize,
        mode = _this$get2.mode,
        min = _this$get2.min,
        max = _this$get2.max;

    var minObject = parseValue(min);
    var maxObject = parseValue(max);

    if (minObject.percent || maxObject.percent) {
      var generateValue = function generateValue(_ref) {
        var percent = _ref.percent,
            op = _ref.op,
            px = _ref.px;

        if (percent) {
          var value = totalSize * percent / 100;

          if (px) {
            px = +px;
            value = op === '-' ? value - px : value + px;
          }

          return value;
        }

        return +px;
      };

      var totalSize = mode === 'horizontal' ? this.element.offsetWidth : this.element.offsetHeight;
      min = generateValue(minObject);
      max = generateValue(maxObject);
    }

    this.min = min;
    this.max = max;
  };

  _proto._restrainValue = function _restrainValue(v) {
    return Math.min(Math.max(v, this.min), this.max);
  };

  _proto._onMoveStart = function _onMoveStart(e) {
    // left mouse key
    if (e.which !== 1) return;
    this.set('_resizing', true);

    this._generateMinMaxValue();

    var _this$get3 = this.get(),
        mode = _this$get3.mode,
        firstSize = _this$get3.firstSize,
        lastSize = _this$get3.lastSize;

    if (lastSize === 'auto') {
      this._changeSizeName = 'firstSize';

      if (mode === 'horizontal') {
        var width = this._restrainValue(this.refs.first.offsetWidth);

        this._x = width - e.clientX;
      } else {
        var height = this._restrainValue(this.refs.first.offsetHeight);

        this._y = height - e.clientY;
      }
    } else {
      this._changeSizeName = 'lastSize';

      if (mode === 'horizontal') {
        var _width = this._restrainValue(this.refs.last.offsetWidth);

        this._x = _width + e.clientX;
      } else {
        var _height = this._restrainValue(this.refs.last.offsetHeight);

        this._y = _height + e.clientY;
      }
    }

    document.addEventListener('mousemove', this._onMove);
    document.addEventListener('mouseup', this._onMoveEnd);
    this.trigger('moveStart', e);
  };

  _proto._onMove = function _onMove(e) {
    e.preventDefault();

    var _this$get4 = this.get(),
        mode = _this$get4.mode;

    var min = this.min,
        max = this.max;
    var size;

    if (this._changeSizeName === 'firstSize') {
      if (mode === 'horizontal') {
        size = this._x + e.clientX;
      } else {
        size = this._y + e.clientY;
      }
    } else {
      if (mode === 'horizontal') {
        size = this._x - e.clientX;
      } else {
        size = this._y - e.clientY;
      }
    }

    size = this._restrainValue(size);
    this.set(this._changeSizeName, size + 'px');
    this.trigger('moving', e);
  };

  _proto._onMoveEnd = function _onMoveEnd(e) {
    if (this.get('_resizing')) {
      document.removeEventListener('mousemove', this._onMove);
      document.removeEventListener('mouseup', this._onMoveEnd);
      this.set('_resizing', false);
      this.trigger('moveEnd', e);
    }
  };

  _proto._destroy = function _destroy() {
    this._onMoveEnd();
  };

  return Split;
}(Intact), _defineProperty(_class2, "template", template), _defineProperty(_class2, "blocks", ['first', 'last', 'drag']), _defineProperty(_class2, "propTypes", {
  mode: ['horizontal', 'vertical'],
  firstSize: String,
  lastSize: String,
  min: [Number, String],
  max: [Number, String]
}), _defineProperty(_class2, "events", {
  moveStart: true,
  moving: true,
  moveEnd: true
}), _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 { Split as default };
var regexp = /(?:(\d+(?:\.\d+)?)%)?([\+\-])?(\d+(?:\.\d+)?)?/;
var cache = {};

function parseValue(v) {
  if (typeof v === 'number') return {
    px: v
  };

  if (!cache[v]) {
    var matches = v.match(regexp) || [];
    cache[v] = {
      percent: matches[1],
      op: matches[2],
      px: matches[3]
    };
  }

  return cache[v];
}

export { Split };