import { getCountryRules } from '@/service/index/api'
import { useRulesStore } from '@/store/rules'
import { useUserStore } from '@/store/user'
import { RuleResult } from '@/types/app-type'

const rulesStore = useRulesStore()
const userStore = useUserStore()

export const requireSubjectList = ref([])
export const optionalSubjectList = ref([])
// 单选 必选学科
export const requireSubject = ref('')
// 任意选择科目
export const optionalSubject = ref([])

export const useRules = () => {
  getCountryRules().then((res) => {
    if (res.code === 200) {
      const results = res.result as RuleResult[]
      rulesStore.setRules(results)
      const matchRule = results.filter((item) => item.p === userStore.userInfo.city.policy)
      const matchRules = matchRule.length > 0 ? matchRule[0].rule : []
      requireSubjectList.value = matchRules.filter((item) => item?.isfirst === 'true')
      optionalSubjectList.value = matchRules.filter((item) => !(item?.isfirst === 'true'))
    }
  })
}
