<template>
    <view class="van-stepper custom-class">
        <view
            v-if="showMinus"
            data-type="minus"
            :style="'width: ' + utils.addUnit(buttonSize) + '; height: ' + utils.addUnit(buttonSize)"
            :class="'minus-class ' + utils.bem('stepper__minus', { disabled: disabled || disableMinus || currentValue <= min })"
            hover-class="van-stepper__minus--hover"
            hover-stay-time="70"
            @tap="onTap"
            @touchstart="onTouchStart"
            @touchend="onTouchEnd"
        />
        <input
            :type="integer ? 'number' : 'digit'"
            :class="'input-class ' + utils.bem('stepper__input', { disabled: disabled || disableInput })"
            :style="'width: ' + utils.addUnit(inputWidth) + '; height: ' + utils.addUnit(buttonSize)"
            :value="currentValue"
            :focus="focus"
            :disabled="disabled || disableInput"
            @input="onInput"
            @focus="onFocus"
            @blur="onBlur"
        />
        <view
            v-if="showPlus"
            data-type="plus"
            :style="'width: ' + utils.addUnit(buttonSize) + '; height: ' + utils.addUnit(buttonSize)"
            :class="'plus-class ' + utils.bem('stepper__plus', { disabled: disabled || disablePlus || currentValue >= max })"
            hover-class="van-stepper__plus--hover"
            hover-stay-time="70"
            @tap="onTap"
            @touchstart="onTouchStart"
            @touchend="onTouchEnd"
        />
    </view>
</template>
<script module="utils" lang="wxs" src="@/node_modules/@vant/weapp/lib/wxs/utils.wxs"></script>
<script>
'use strict';
var __assign =
    (this && this.__assign) ||
    function () {
        __assign =
            Object.assign ||
            function (t) {
                for (var s, i = 1, n = arguments.length; i < n; i++) {
                    s = arguments[i];
                    for (var p in s) {
                        if (Object.prototype.hasOwnProperty.call(s, p)) {
                            t[p] = s[p];
                        }
                    }
                }
                return t;
            };
        return __assign.apply(this, arguments);
    };
Object.defineProperty(exports, '__esModule', {
    value: true
});
var component_1 = require('../common/component');
var utils_1 = require('../common/utils');
var LONG_PRESS_START_TIME = 600;
var LONG_PRESS_INTERVAL = 200;
// add num and avoid float number
function add(num1, num2) {
    var cardinal = Math.pow(10, 10);
    return Math.round((num1 + num2) * cardinal) / cardinal;
}
function equal(value1, value2) {
    return String(value1) === String(value2);
}
component_1.VantComponent({
    field: true,
    classes: ['input-class', 'plus-class', 'minus-class'],
    props: {
        value: {
            type: null,
            observer: function (value) {
                if (!equal(value, this.currentValue)) {
                    this.setData({
                        currentValue: this.format(value)
                    });
                }
            }
        },
        integer: {
            type: Boolean,
            observer: 'check'
        },
        disabled: Boolean,
        inputWidth: null,
        buttonSize: null,
        asyncChange: Boolean,
        disableInput: Boolean,
        decimalLength: {
            type: Number,
            value: null,
            observer: 'check'
        },
        min: {
            type: null,
            value: 1,
            observer: 'check'
        },
        max: {
            type: null,
            value: Number.MAX_SAFE_INTEGER,
            observer: 'check'
        },
        step: {
            type: null,
            value: 1
        },
        showPlus: {
            type: Boolean,
            value: true
        },
        showMinus: {
            type: Boolean,
            value: true
        },
        disablePlus: Boolean,
        disableMinus: Boolean,
        longPress: {
            type: Boolean,
            value: true
        }
    },
    data: {
        currentValue: ''
    },
    created: function () {
        this.setData({
            currentValue: this.format(this.value)
        });
    },
    methods: {
        check: function () {
            var val = this.format(this.currentValue);
            if (!equal(val, this.currentValue)) {
                this.setData({
                    currentValue: val
                });
            }
        },
        isDisabled: function (type) {
            if (type === 'plus') {
                return this.disabled || this.disablePlus || this.currentValue >= this.max;
            }
            return this.disabled || this.disableMinus || this.currentValue <= this.min;
        },
        onFocus: function (event) {
            this.$emit('focus', event.detail);
        },
        onBlur: function (event) {
            var value = this.format(event.detail.value);
            this.emitChange(value);
            this.$emit(
                'blur',
                __assign(__assign({}, event.detail), {
                    value: value
                })
            );
        },
        // filter illegal characters
        filter: function (value) {
            value = String(value).replace(/[^0-9.-]/g, '');
            if (this.integer && value.indexOf('.') !== -1) {
                value = value.split('.')[0];
            }
            return value;
        },
        // limit value range
        format: function (value) {
            value = this.filter(value);
            // format range
            value = value === '' ? 0 : +value;
            value = Math.max(Math.min(this.max, value), this.min);
            // format decimal
            if (utils_1.isDef(this.decimalLength)) {
                value = value.toFixed(this.decimalLength);
            }
            return value;
        },
        onInput: function (event) {
            var _a = (event.detail || {}).value;
            var value = _a === void 0 ? '' : _a; // allow input to be empty
            if (value === '') {
                return;
            }
            var formatted = this.filter(value);
            // limit max decimal length
            if (utils_1.isDef(this.decimalLength) && formatted.indexOf('.') !== -1) {
                var pair = formatted.split('.');
                formatted = pair[0] + '.' + pair[1].slice(0, this.decimalLength);
            }
            this.emitChange(formatted);
        },
        emitChange: function (value) {
            if (!this.asyncChange) {
                this.setData({
                    currentValue: value
                });
            }
            this.$emit('change', value);
        },
        onChange: function () {
            var type = this.type;
            if (this.isDisabled(type)) {
                this.$emit('overlimit', type);
                return;
            }
            var diff = type === 'minus' ? -this.step : +this.step;
            var value = this.format(add(+this.currentValue, diff));
            this.emitChange(value);
            this.$emit(type);
        },
        longPressStep: function () {
            var that = this;
            this.longPressTimer = setTimeout(function () {
                that.onChange();
                that.longPressStep();
            }, LONG_PRESS_INTERVAL);
        },
        onTap: function (event) {
            var type = event.currentTarget.dataset.type;
            this.type = type;
            this.onChange();
        },
        onTouchStart: function (event) {
            var that = this;
            if (!this.longPress) {
                return;
            }
            clearTimeout(this.longPressTimer);
            var type = event.currentTarget.dataset.type;
            this.type = type;
            this.isLongPress = false;
            this.longPressTimer = setTimeout(function () {
                that.isLongPress = true;
                that.onChange();
                that.longPressStep();
            }, LONG_PRESS_START_TIME);
        },
        onTouchEnd: function () {
            if (!this.longPress) {
                return;
            }
            clearTimeout(this.longPressTimer);
        }
    }
});
</script>
<style>
@import './index.css';
</style>
