const types = {
    control: {
        unload: 'removeControl' // 删除控件方法---
    },
    layer: {
        unload: 'removeLayer' // 删除图层方法
    },
    overlay: {
        unload: 'removeOverlay' // 删除叠置覆盖物方法
    },
    interaction: {
        unload: 'removeInteraction' // 删除交互控件方法
    }
}

const getParent = $component => ($component.abstract || $component.$el === $component.$children[0].$el) ? getParent($component.$parent) : $component

function destroyInstance() {
    const { unload, renderByParent, $parent } = this
    if (renderByParent) {
        $parent.reload()
    }
    unload()
}

class Mixin {
    constructor(prop) {
        this.methods = {
            ready(obj) {
                this.map = obj.map
                this.initComponent(obj.map)
            },
            initComponent(map) {
                // 组件有了地图对象以后才执行相应的操作----load方法执行map加载完成后的处理逻辑---
                this.load()
                    // 把地图对象分发出去-----
                this.$emit('ready', {
                    map
                })
            },
            transmitEvent(e) {
                this.$emit(e.type.replace(/^on/, ''), e)
            },
            // 重新加载组件-----
            reload() {
                // 当页面视图更新结束----
                this.map && this.$nextTick(() => {
                    // 卸载先前组件
                    this.unload()
                        // 组件卸载完成页面跟新后重新加载组件---
                    this.$nextTick(this.load)
                })
            },
            // 卸载组件师删除相应的控件、交互组件----
            unload() {
                const { map, originInstance } = this
                try {
                    map[types[prop.type].unload](originInstance)
                } catch (e) {}
            }
        }
        this.computed = {
                // 计算属性----
                renderByParent() {
                    return this.$parent.preventChildrenRender
                }
            }
            // 钩子函数----挂载成功后执行的函数
        this.mounted = function() {
                const $parent = getParent(this.$parent)
                const { ready } = this
                if ($parent.olmap) {
                    this.map = $parent.olmap
                    this.initComponent($parent.olmap)
                } else {
                    $parent.$on('ready', ready)
                }
            }
            // vue钩子函数---
        this.destroyed = destroyInstance
        this.beforeDestroy = destroyInstance
    }
}

export default type => new Mixin({ type })