import {createInjectDefaultClosure} from "@/utils";
import {confirmAsync} from "@/utils/uni-helper";

export default {
    // Use this way, computed properties can keep `REACTIVE` availability.
    inject: {
        // data inject
        'getPrevData': createInjectDefaultClosure({}),
        'getFilter': createInjectDefaultClosure({}),
        'getShortcut': createInjectDefaultClosure([]),
        'getConfig': createInjectDefaultClosure({}),
        'getCart': createInjectDefaultClosure([]),
        'getCommit': createInjectDefaultClosure({}),
        // methods inject
        'handleCartAdd': createInjectDefaultClosure(true),
        'handleCartRemove': createInjectDefaultClosure(true),
        'handleCartClear': createInjectDefaultClosure(true),
        'handleCommit': createInjectDefaultClosure(true)
    },
    computed: {
        pageData() {
            return this.getPrevData()
        },
        filter() {
            return this.getFilter()
        },
        shortcut() {
            return this.getShortcut()
        },
        config() {
            return this.getConfig()
        },
        cart() {
            return this.getCart()
        },
        commit() {
            return this.getCommit()
        }
    },
    methods: {
        async couldChangeFilter(op, confirmedAction) {
            let filterKeys = op.filterKey || op?.id?.split('_')[0]
            if (!filterKeys && op.options?.length) filterKeys = op.options.map(o => o.filterKey)
            if (filterKeys) filterKeys = [].concat(filterKeys)
            if (!filterKeys) throw new Error('must find filter keys here')
            // 这几个筛选条件会影响专业，要改变必须清空志愿表
            const confirmKeys = this.config.confirmChangeFilterKeys || []
            if (this.cart.length && filterKeys.some(k => confirmKeys.includes(k))) {
                await confirmAsync('更改筛选条件需要清空当前志愿表\r\n确认清空？！')
                this.handleCartClear()
                if (typeof confirmedAction === 'function') confirmedAction()
            }
        }
    }
}