import { DatePickerDefaultProps } from './props';
import dayjs from 'dayjs';
import equal from 'fast-deep-equal';
import { getRangeData, getDateByValue, getValueByDate, getValidValue, isEqualDate, } from './util';
import fmtEvent from '../_util/fmtEvent';
import mixinValue from '../mixins/value';
import '../_util/assert-component2';
var component2 = my.canIUse('component2');
Component({
    props: DatePickerDefaultProps,
    pickerVisible: false,
    data: function () {
        return {
            currentValue: [],
            columns: [],
            forceUpdate: 0, // 强制更新picker组件，已知需处理的情况：value超限，但是需要更新format，由于picker的参数均未变化，无法触发picker的渲染
        };
    },
    mixins: [
        mixinValue({
            transformValue: function (value) {
                return {
                    value: value ? dayjs(value).toDate() : undefined,
                    needUpdate: true,
                };
            },
        }),
    ],
    didMount: function () {
        this.pickerVisible = false;
    },
    deriveDataFromProps: function (nextProps) {
        this.updateValue(this.props, nextProps);
    },
    didUpdate: function (prevProps) {
        if (!component2) {
            this.updateValue(prevProps, this.props);
        }
    },
    methods: {
        updateValue: function (prevProps, currentProps) {
            if (!isEqualDate(prevProps.value, currentProps.value)) {
                this.setData({
                    forceUpdate: this.data.forceUpdate + 1,
                });
                // 展开状态才更新picker的数据，否则下次triggerVisible触发
                if (this.pickerVisible) {
                    this.setCurrentValue(currentProps);
                }
            }
        },
        // 当前选中的picker值，处理无cValue时的情况，优先取当前时间，不在时间范围内取开始时间
        getCurrentValueWithCValue: function (currentProps) {
            var realValue = this.getValue();
            var min = currentProps.min, max = currentProps.max, precision = currentProps.precision;
            if (realValue) {
                return getValueByDate(realValue, precision);
            }
            else {
                var now = new Date();
                if (!(min && dayjs(now).isBefore(dayjs(min), precision)) &&
                    !(max && dayjs(now).isAfter(dayjs(max), precision))) {
                    return getValueByDate(now, precision);
                }
                else {
                    return getValueByDate(this.getMin(min).toDate(), precision);
                }
            }
        },
        getMin: function (min) {
            return min ? dayjs(min) : dayjs().subtract(10, 'year');
        },
        getMax: function (max) {
            return max ? dayjs(max) : dayjs().add(10, 'year');
        },
        /**
         * didUpdate、弹窗打开触发
         */
        setCurrentValue: function (currentProps) {
            var _this = this;
            var currentValue = this.getCurrentValueWithCValue(currentProps);
            var newColumns = this.generateData(currentValue, currentProps);
            if (!equal(newColumns, this.data.columns)) {
                this.setData({
                    columns: newColumns,
                }, function () {
                    _this.setData({
                        currentValue: currentValue,
                    });
                });
            }
            else {
                this.setData({
                    currentValue: currentValue,
                });
            }
        },
        // 生成选项数据，didmound、picker change、打开弹窗触发
        generateData: function (currentValue, currentProps) {
            var precision = currentProps.precision, propsMin = currentProps.min, propsMax = currentProps.max;
            var min = this.getMin(propsMin);
            var max = this.getMax(propsMax);
            if (max < min) {
                return [];
            }
            var currentPickerDay = dayjs();
            if (currentValue.length > 0) {
                currentPickerDay = dayjs(getDateByValue(currentValue));
            }
            if (currentPickerDay < min || currentPickerDay > max) {
                currentPickerDay = min;
            }
            var newColumns = getRangeData(precision, min, max, currentPickerDay, this.onFormatLabel.bind(this));
            return newColumns;
        },
        onFormatLabel: function (type, value) {
            var onFormatLabel = this.props.onFormatLabel;
            var formatValueByProps = onFormatLabel && onFormatLabel(type, value);
            if (typeof formatValueByProps !== 'undefined') {
                return String(formatValueByProps);
            }
            return this.defaultFormatLabel(type, value);
        },
        defaultFormatLabel: function (type, value) {
            var suffixMap = {
                year: '年',
                month: '月',
                day: '日',
                hour: '时',
                minute: '分',
                second: '秒',
            };
            return "".concat(value).concat(suffixMap[type]);
        },
        onChange: function (selectedIndex) {
            var _this = this;
            selectedIndex = getValidValue(selectedIndex);
            var _a = this.props, onPickerChange = _a.onPickerChange, format = _a.format, precision = _a.precision;
            var date = getDateByValue(selectedIndex);
            var min = this.getMin(this.props.min);
            var max = this.getMax(this.props.max);
            if (dayjs(date).isBefore(min)) {
                date = min.toDate();
                selectedIndex = getValueByDate(date, precision);
            }
            if (dayjs(date).isAfter(max)) {
                date = max.toDate();
                selectedIndex = getValueByDate(date, precision);
            }
            var newColumns = this.generateData(selectedIndex, this.props);
            if (!equal(newColumns, this.data.columns)) {
                this.setData({
                    columns: newColumns,
                }, function () {
                    _this.setData({ currentValue: selectedIndex });
                    if (onPickerChange) {
                        var date_1 = getDateByValue(selectedIndex);
                        onPickerChange(date_1, dayjs(date_1).format(format), fmtEvent(_this.props));
                    }
                });
            }
            else {
                this.setData({ currentValue: selectedIndex });
                if (onPickerChange) {
                    var date_2 = getDateByValue(selectedIndex);
                    onPickerChange(date_2, dayjs(date_2).format(format), fmtEvent(this.props));
                }
            }
        },
        onCancel: function (e) {
            var onCancel = this.props.onCancel;
            if (onCancel) {
                onCancel(fmtEvent(this.props, e));
            }
        },
        onOk: function () {
            var currentValue = this.data.currentValue;
            var format = this.props.format;
            var date = getDateByValue(currentValue);
            if (!this.isControlled()) {
                this.update(date);
            }
            if (this.props.onOk) {
                this.props.onOk(date, dayjs(date).format(format), fmtEvent(this.props));
            }
        },
        defaultFormat: function (value, valueStr) {
            if (this.props.format && valueStr) {
                return valueStr;
            }
            return '';
        },
        onFormat: function () {
            var _a = this.props, onFormat = _a.onFormat, format = _a.format;
            var realValue = this.getValue();
            var formatValueByProps = onFormat &&
                onFormat(realValue, realValue ? dayjs(realValue).format(format) : null);
            if (typeof formatValueByProps !== 'undefined') {
                return formatValueByProps;
            }
            return this.defaultFormat(realValue, realValue ? dayjs(realValue).format(format) : null);
        },
        onVisibleChange: function (visible) {
            this.pickerVisible = visible;
            var onVisibleChange = this.props.onVisibleChange;
            if (visible) {
                this.setCurrentValue(this.props);
            }
            if (onVisibleChange) {
                onVisibleChange(visible, fmtEvent(this.props));
            }
        },
    },
});
