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() {
                const $parent = getParent(this.$parent)
                const map = this.map = $parent.olmap
                this.$rootComponent = $parent
                this.load()
                    // this.$emit('ready', {
                    //     map
                    // })
            },
            transmitEvent(e) {
                this.$emit(e.type.replace(/^on/, ''), e)
            },
            reload() {
                this && 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 map = $parent.map
            const { ready } = this
            map ? ready() : $parent.$on('ready', ready)
        }
        this.destroyed = destroyInstance
        this.beforeDestroy = destroyInstance
    }
}

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