'use strict';
Object.defineProperty(exports, '__esModule', {
    value: true
});
exports.default = popupMixin;
var _classNames3 = _interopRequireDefault(require('./classNames'));
var _eventsMixin = _interopRequireDefault(require('./eventsMixin'));
function _interopRequireDefault(e) {
    return e && e.__esModule
        ? e
        : {
              default: e
          };
}
function ownKeys(t, e) {
    var i = Object.keys(t);
    if (Object.getOwnPropertySymbols) {
        var n = Object.getOwnPropertySymbols(t);
        if (e) {
            n = n.filter(function (e) {
                return Object.getOwnPropertyDescriptor(t, e).enumerable;
            });
        }
        i.push.apply(i, n);
    }
    return i;
}
function _objectSpread(t) {
    for (var e = 1; e < arguments.length; e++) {
        if (null != arguments[e]) {
            var i = arguments[e];
        } else {
            var i = {};
        }
        e % 2
            ? ownKeys(i, true).forEach(function (e) {
                  _defineProperty(t, e, i[e]);
              })
            : Object.getOwnPropertyDescriptors
            ? Object.defineProperties(t, Object.getOwnPropertyDescriptors(i))
            : ownKeys(i).forEach(function (e) {
                  Object.defineProperty(t, e, Object.getOwnPropertyDescriptor(i, e));
              });
    }
    return t;
}
function _defineProperty(e, t, i) {
    t in e
        ? Object.defineProperty(e, t, {
              value: i,
              enumerable: true,
              configurable: true,
              writable: true
          })
        : (e[t] = i);
    return e;
}
var DEFAULT_TRIGGER = 'onClick';
var CELL_NAME = '../cell/index';
var FIELD_NAME = '../field/index';
var defaultToolbar = {
    title: '请选择',
    cancelText: '取消',
    confirmText: '确定'
};
var defaultEvents = {
    onChange: function () {},
    onConfirm: function () {},
    onCancel: function () {},
    onVisibleChange: function () {},
    onValueChange: function () {}
};
var defaultPlatformProps = {
    labelPropName: 'label',
    format: function (e, t) {
        return Array.isArray(e.displayValue) ? e.displayValue.join(',') : e.displayValue;
    }
};
var defaultFieldNames = {
    label: 'label',
    value: 'value',
    children: 'children'
};
function popupMixin() {
    if (0 < arguments.length && void 0 !== arguments[0]) {
        var i = arguments[0];
    } else {
        var i = '#wux-picker';
    }
    var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : defaultPlatformProps;
    return {
        data() {
            return {
                mounted: false,
                popupVisible: false,
                inputValue: []
            };
        },
        mixins: [
            (0, _eventsMixin.default)({
                defaultEvents: defaultEvents
            })
        ],
        props: {
            toolbar: {
                type: Object,
                value: defaultToolbar
            },
            trigger: {
                type: String,
                value: DEFAULT_TRIGGER
            },
            defaultVisible: {
                type: Boolean,
                value: false
            },
            visible: {
                type: Boolean,
                value: false
            },
            controlled: {
                type: Boolean,
                value: false
            },
            disabled: {
                type: Boolean,
                value: false
            }
        },
        methods: {
            setVisibleState: function (t, e) {
                var that = this;
                var n = 1 < arguments.length && void 0 !== e ? e : function () {};
                if (this.popupVisible !== t) {
                    var a = {
                        mounted: true,
                        inputValue: this.value,
                        popupVisible: t
                    };
                    this.setData(
                        t
                            ? a
                            : {
                                  popupVisible: t
                              },
                        function () {
                            if (t && that.hasFieldDecorator) {
                                var e = that.getFieldElem();
                                if (e) {
                                    e.changeValue(e.data.value);
                                }
                            }
                            n();
                        }
                    );
                }
            },
            fireVisibleChange: function (e) {
                if (this.popupVisible !== e) {
                    this.controlled || this.setVisibleState(e);
                    this.setScrollValue(void 0);
                    this.$emit('visibleChange', {
                        detail: {
                            visible: e
                        }
                    });
                }
            },
            open: function () {
                this.fireVisibleChange(true);
            },
            close: function (e) {
                if ('function' == typeof e) {
                    var t = this.getPickerValue(this.scrollValue || this.inputValue);
                    e.call(this, this.formatPickerValue(t));
                }
                this.fireVisibleChange(false);
            },
            onClosed: function () {
                this.picker = null;
                this.setData({
                    mounted: false,
                    inputValue: null
                });
            },
            onConfirm: function () {
                var that = this;
                this.close(function (e) {
                    that.$emit('change', {
                        detail: e
                    });
                    that.$emit('confirm', {
                        detail: e
                    });
                });
            },
            onCancel: function () {
                var that = this;
                this.close(function (e) {
                    return that.$emit('cancel', {
                        detail: e
                    });
                });
            },
            onValueChange: function (e) {
                if (this.mounted) {
                    var t = e.detail.value;
                    this.cascade ? this.setCasecadeScrollValue(t) : this.setScrollValue(t);
                    this.updated(t, true);
                    this.$emit('valueChange', {
                        detail: this.formatPickerValue(e.detail)
                    });
                }
            },
            getPickerValue: function (e) {
                var t = 0 < arguments.length && void 0 !== e ? e : this.inputValue;
                this.picker = this.picker || this.zpSelectComponent(i);
                return this.picker && this.picker.getValue(t);
            },
            formatPickerValue: function (e) {
                return _objectSpread({}, e, _defineProperty({}, t.labelPropName, t.format(e, this)));
            },
            getFieldElem: function () {
                return (this.field = this.field || this.getRelationNodes(FIELD_NAME)[0]);
            },
            setChildProps: function () {
                var that = this;
                if (!this.disabled) {
                    var e = this.getRelationNodes(CELL_NAME);
                    var t = this.trigger;
                    var a = void 0 === t ? DEFAULT_TRIGGER : t;
                    if (0 < e.length) {
                        e.forEach(function (e) {
                            var t = e.data.inputEvents;
                            var i = e.data.oriInputEvents || _objectSpread({}, t);
                            t[a] = function () {
                                if (i && i[a]) {
                                    i[a].apply(i, arguments);
                                }
                                that.onTriggerClick();
                            };
                            e.setData({
                                oriInputEvents: i,
                                inputEvents: t
                            });
                        });
                    }
                }
            },
            onTriggerClick: function () {
                this.fireVisibleChange(!this.popupVisible);
            },
            noop: function () {},
            updated: function (e, t) {
                (this.hasFieldDecorator && !t) ||
                    (this.inputValue !== e &&
                        this.setData({
                            inputValue: e
                        }));
            },
            setScrollValue: function (e) {
                this.scrollValue = e;
            },
            setCasecadeScrollValue: function (e) {
                if (e && this.scrollValue) {
                    var t = this.scrollValue.length;
                    if (t === e.length && this.scrollValue[t - 1] === e[t - 1]) {
                        return;
                    }
                }
                this.setScrollValue(e);
            }
        },
        lifetimes: {
            ready: function () {
                var e = this;
                var t = e.defaultVisible;
                var i = e.visible;
                var n = e.controlled;
                var a = (e.value, n ? i : t);
                if ('defaultFieldNames' in this) {
                    this.setData({
                        fieldNames: Object.assign({}, defaultFieldNames, this.defaultFieldNames)
                    });
                }
                this.mounted = true;
                this.scrollValue = void 0;
                this.setVisibleState(a);
                this.setChildProps();
            },
            detached: function () {
                this.mounted = false;
            }
        },
        definitionFilter: function (e) {
            var t;
            Object.assign(
                (e.relations = e.relations || {}),
                (_defineProperty((t = {}), CELL_NAME, {
                    type: 'child',
                    observer: function () {
                        this.setChildProps();
                    }
                }),
                _defineProperty(t, FIELD_NAME, {
                    type: 'ancestor'
                }),
                t)
            );
            Object.assign((e.computed = e.computed || {}), {
                classes: [
                    'prefixCls',
                    function (e) {
                        return {
                            wrap: (0, _classNames3.default)(e),
                            toolbar: ''.concat(e, '__toolbar'),
                            inner: ''.concat(e, '__inner'),
                            cancel: (0, _classNames3.default)(''.concat(e, '__button'), _defineProperty({}, ''.concat(e, '__button--cancel'), true)),
                            confirm: (0, _classNames3.default)(''.concat(e, '__button'), _defineProperty({}, ''.concat(e, '__button--confirm'), true)),
                            hover: ''.concat(e, '__button--hover'),
                            title: ''.concat(e, '__title')
                        };
                    }
                ]
            });
            Object.assign((e.observers = e.observers || {}), {
                visible: function (e) {
                    if (this.controlled) {
                        this.setVisibleState(e);
                    }
                },
                value: function (e) {
                    this.updated(e);
                }
            });
        }
    };
}
