import { __awaiter, __generator } from "tslib";
import { CascaderDefaultProps } from './props';
import fmtEvent from '../../_util/fmtEvent';
import equal from 'fast-deep-equal';
import mixinValue from '../../mixins/value';
var component2 = my.canIUse('component2');
Component({
    props: CascaderDefaultProps,
    data: function () {
        return {
            currentValue: [],
            columns: [], // 可选项，didmound、弹窗打开、picker变化时更新
        };
    },
    mixins: [mixinValue()],
    onInit: function () {
        this.initColumns();
    },
    didMount: function () {
        if (!component2) {
            this.initColumns();
        }
    },
    deriveDataFromProps: function (nextProps) {
        this.updateValue(this.props, nextProps);
    },
    didUpdate: function (prevProps) {
        if (!component2) {
            this.updateValue(prevProps, this.props);
        }
    },
    methods: {
        initColumns: function () {
            var realValue = this.getValue();
            var columns = this.getterColumns(realValue, this.props.options);
            // 首次无需校验value有效性，onOk时会校验
            this.setData({ columns: columns });
        },
        updateValue: function (prevProps, currentProps) {
            var value = currentProps.value, options = currentProps.options;
            var _a = this.data, columns = _a.columns, currentValue = _a.currentValue;
            if (options !== prevProps.options) {
                var newData = {};
                if (!equal(value, prevProps.value)) {
                    var newColumns = this.getterColumns(value, currentProps.options);
                    newData.columns = newColumns;
                    var currentValue_1 = this.getValidValue(value, newColumns);
                    newData.currentValue = currentValue_1;
                }
                else {
                    var newColumns = this.getterColumns(currentValue, currentProps.options);
                    newData.columns = newColumns;
                }
                this.setData(newData);
            }
            else {
                if (!equal(value, prevProps.value)) {
                    var realValue = this.getValue();
                    var currentValue_2 = this.getValidValue(realValue, columns);
                    this.setData({ currentValue: currentValue_2 });
                }
            }
        },
        getterColumns: function (value, options) {
            var getColumns = function (options, value, columns) {
                var _a;
                if (columns === void 0) { columns = []; }
                columns.push(options.map(function (v) { return ({ value: v.value, label: v.label }); }));
                var currentOption = options.find(function (v) { return v.value === (value === null || value === void 0 ? void 0 : value[columns.length - 1]); }) ||
                    options[0];
                if (((_a = currentOption === null || currentOption === void 0 ? void 0 : currentOption.children) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                    return getColumns(currentOption.children, value, columns);
                }
                return columns;
            };
            return getColumns(options, value);
        },
        // 获取有效value，若从x项开始在columns里找不到，则从此项开始都选第一条
        getValidValue: function (value, columns) {
            var result = [];
            var _loop_1 = function (i) {
                if (!columns[i].some(function (v) { return v.value === (value === null || value === void 0 ? void 0 : value[i]); })) {
                    result.push.apply(result, columns.slice(i).map(function (v) { return v[0].value; }));
                    return "break";
                }
                else {
                    result[i] = value[i];
                }
            };
            for (var i = 0; i < columns.length; i++) {
                var state_1 = _loop_1(i);
                if (state_1 === "break")
                    break;
            }
            return result;
        },
        getOptionByValue: function (value) {
            var _a;
            var options = this.props.options;
            if (!((value === null || value === void 0 ? void 0 : value.length) > 0))
                return null;
            var result = [];
            var item = options.find(function (v) { return v.value === value[0]; });
            var _loop_2 = function (i) {
                if (!item) {
                    return { value: null };
                }
                result.push({
                    value: item.value,
                    label: item.label,
                });
                item = (_a = item.children) === null || _a === void 0 ? void 0 : _a.find(function (v) { return v.value === value[i + 1]; });
            };
            for (var i = 0; i < value.length; i++) {
                var state_2 = _loop_2(i);
                if (typeof state_2 === "object")
                    return state_2.value;
            }
            return result;
        },
        onChange: function (selectedValue) {
            var _a = this.props, onChange = _a.onChange, options = _a.options;
            var columns = this.data.columns;
            var newColumns = this.getterColumns(selectedValue, options);
            // columns没变化说明selectedValue在范围内，无需重置
            var newData = {};
            if (!equal(columns, newColumns)) {
                selectedValue = this.getValidValue(selectedValue, newColumns);
                newData.columns = newColumns;
            }
            newData.currentValue = selectedValue;
            this.setData(newData);
            if (onChange) {
                onChange(selectedValue, this.getOptionByValue(selectedValue), fmtEvent(this.props));
            }
        },
        onOk: function () {
            return __awaiter(this, void 0, void 0, function () {
                var _a, currentValue, columns, onOk, validValue;
                return __generator(this, function (_b) {
                    _a = this.data, currentValue = _a.currentValue, columns = _a.columns;
                    onOk = this.props.onOk;
                    validValue = this.getValidValue(currentValue, columns);
                    if (!this.isControlled()) {
                        this.update(validValue);
                    }
                    if (onOk) {
                        onOk(validValue, this.getOptionByValue(validValue), fmtEvent(this.props));
                    }
                    return [2 /*return*/];
                });
            });
        },
        onVisibleChange: function (visible) {
            var _a = this.props, onVisibleChange = _a.onVisibleChange, options = _a.options;
            var columns = this.data.columns;
            var realValue = this.getValue();
            if (visible) {
                var newColumns = this.getterColumns(realValue, options);
                var currentValue = this.getValidValue(realValue, newColumns);
                var newData = { currentValue: currentValue };
                if (!equal(columns, newColumns)) {
                    newData.columns = newColumns;
                }
                this.setData(newData);
            }
            if (onVisibleChange) {
                onVisibleChange(visible, fmtEvent(this.props));
            }
        },
        defaultFormat: function (value, options) {
            if (options) {
                return options.map(function (v) { return v.label; }).join('');
            }
            return '';
        },
        onFormat: function () {
            var realValue = this.getValue();
            var onFormat = this.props.onFormat;
            var formatValueByProps = onFormat && onFormat(realValue, this.getOptionByValue(realValue));
            if (typeof formatValueByProps !== 'undefined') {
                return formatValueByProps;
            }
            return this.defaultFormat(realValue, this.getOptionByValue(realValue));
        },
        onCancel: function (e) {
            var onCancel = this.props.onCancel;
            if (onCancel) {
                onCancel(fmtEvent(this.props, e));
            }
        },
    },
});
