/**version-1.0*/
let modules = {};

modules = {
	install: function(Vue) {
		Vue.directive('LimitNumber', {
			bind(el, binding, vnode) {
				let scale = binding.value != null ? Number(binding.value) : 2;
				let min = vnode.componentInstance ? vnode.componentInstance.min : null;
				let negative = vnode.componentInstance && vnode.componentInstance.$attrs ?
					vnode.componentInstance.$attrs.negative : false;
				negative = negative == "true" || negative == true;

				if (Number(min) >= 0) {
					negative = false;
				}

				el.onkeyup = (event) => {
					let regular;
					if (scale == 0) {
						if (!negative) {
							regular = /[^\d]/g;
						} else {
							regular = /[^\d\-]/g;
						}
					} else {
						if (!negative) {
							regular = /[^\d\.]/g;
						} else {
							regular = /[^\d\.-]/g;
						}
					}
					let value = event.target.value.replaceAll(
						regular,
						""
					);
					event.target.value = value;
				};
				el.onkeypress = (event) => {
					let input = event.target;
					let selectionStart = input.selectionStart;
					let selectionEnd = input.selectionEnd;

					let keyCode = event.keyCode || event.which;
					let value = event.target.value;

					let keyValue = String.fromCharCode(keyCode);
					if (keyCode === 46 && scale === 0) {
						return false;
					}

					if (keyCode === 45 && !negative) {
						return false;
					}

					let res = keyCode === 45 || keyCode === 46 || (/[\d]/.test(keyValue)) || event
						.which === 8;
					if (res) {
						let size = value.length;
						if ((keyValue == "-" && value == "")) {
							res = true;
						} else {
							let newValue;

							if (selectionStart == selectionEnd) {
								if (selectionStart == 0) {
									newValue = keyValue + '' + value
								} else if (selectionStart == size - 1) {
									newValue = value + '' + keyValue;
								} else {
									newValue = value.substring(0, selectionStart) + keyValue + value
										.slice(selectionStart);
								}
							} else {
								if (selectionStart == 0) {
									if (selectionEnd == size - 1) {
										newValue = keyValue;
									} else {
										newValue = keyValue + value.slice(selectionEnd);
									}
								} else {
									if (selectionEnd == size - 1) {
										newValue = value.substring(0, selectionStart) + keyValue;
									} else {
										newValue = value.substring(0, selectionStart) + keyValue +
											value.slice(selectionEnd);
									}
								}
							}
							let i = newValue.indexOf(".");
							if (i == 0) {
								res = false;
							} else if (keyValue == "." && i == newValue.length - 1) {
								res = true;
							} else {
								if (newValue == "-") {
									return true;
								}
								res = !isNaN(newValue);
								if (res) {
									if (newValue.indexOf('.') != -1) {
										var str_ = newValue.substr(newValue.indexOf('.') + 1);
										if (str_.length > scale) {
											if (keyValue == ".") {
												let val = Number(newValue.substr(0, newValue
													.indexOf(
														'.') + 1 + scale));
												event.target.value = val;
											}
											return false;
										}
									}
								}
							}
						}

					}
					return res;
				}
			}
		})
	}
};

export default modules
