import { __spreadArray } from "tslib";
import { SelectorDefaultProps } from './props';
import mixinValue from '../mixins/value';
import fmtEvent from '../_util/fmtEvent';
import '../_util/assert-component2';
Component({
    mixins: [mixinValue()],
    props: SelectorDefaultProps,
    methods: {
        onChange: function (e) {
            var event = fmtEvent(this.props);
            var _a = e.currentTarget.dataset, disabled = _a.disabled, value = _a.value;
            var _b = this.props, multiple = _b.multiple, options = _b.options, maxSelectedCount = _b.maxSelectedCount, minSelectedCount = _b.minSelectedCount, onSelectMax = _b.onSelectMax, onSelectMin = _b.onSelectMin, onChange = _b.onChange;
            if (disabled || this.props.disabled) {
                return;
            }
            if (multiple) {
                var currentValue_1 = this.getValue() || [];
                if (currentValue_1.indexOf(value) > -1) {
                    if (!isNaN(maxSelectedCount) &&
                        currentValue_1.length >= maxSelectedCount) {
                        if (onSelectMax) {
                            onSelectMax(value, options.find(function (v) { return v.value === value; }), event);
                        }
                        return;
                    }
                    currentValue_1 = currentValue_1.filter(function (v) { return v !== value; });
                }
                else {
                    if (!isNaN(minSelectedCount) &&
                        currentValue_1.length <= minSelectedCount) {
                        if (onSelectMin) {
                            onSelectMin(value, options.find(function (v) { return v.value === value; }), event);
                        }
                        return;
                    }
                    currentValue_1 = __spreadArray(__spreadArray([], currentValue_1, true), [value], false);
                }
                if (!this.isControlled()) {
                    this.update(currentValue_1);
                }
                if (onChange) {
                    onChange(currentValue_1, options.filter(function (v) { return currentValue_1.indexOf(v.value) > -1; }), fmtEvent(this.props));
                }
            }
            else {
                if (value === this.getValue()) {
                    if (minSelectedCount === 1) {
                        if (onSelectMin) {
                            onSelectMin(value, options.find(function (v) { return v.value === value; }), event);
                        }
                        return;
                    }
                    if (!this.isControlled()) {
                        this.update(undefined);
                    }
                    if (onChange) {
                        onChange(undefined, undefined, fmtEvent(this.props));
                    }
                }
                else {
                    if (!this.isControlled()) {
                        this.update(value);
                    }
                    if (onChange) {
                        onChange(value, options.find(function (v) { return v.value === value; }), fmtEvent(this.props));
                    }
                }
            }
        },
    },
});
