import { getAllSites } from '@/api/ems/site.js'

const createSiteTypeOptions = () => ([
	{
		text: '储能',
		value: 'cn',
		children: []
	},
	{
		text: '光能',
		value: 'gn',
		children: []
	},
	{
		text: '岸电',
		value: 'ad',
		children: []
	}
])

const site = {
	state: {
		siteOptions: [],
		siteTypeOptions: [],
		currentSiteId: '',
		loaded: false
	},
	mutations: {
		SET_SITE_OPTIONS: (state, siteOptions) => {
			state.siteOptions = siteOptions || []
		},
		SET_SITE_TYPE_OPTIONS: (state, siteTypeOptions) => {
			state.siteTypeOptions = siteTypeOptions || createSiteTypeOptions()
		},
		SET_CURRENT_SITE_ID: (state, siteId) => {
			state.currentSiteId = siteId || ''
		},
		SET_SITE_LOADED: (state, loaded) => {
			state.loaded = !!loaded
		}
	},
	actions: {
		LoadSites({ state, rootState, commit }, payload = {}) {
			const { force = false } = payload
			if (state.loaded && !force) {
				return Promise.resolve({
					siteOptions: state.siteOptions,
					siteTypeOptions: state.siteTypeOptions,
					siteId: state.currentSiteId
				})
			}
			return getAllSites().then(response => {
				const belongSite = rootState?.user?.belongSite || []
				const canAccessAll = !belongSite || belongSite.length === 0 || belongSite.includes('all')
				const siteOptions = (response?.data || []).filter(item => {
					const siteId = item.siteId || ''
					return canAccessAll || belongSite.includes(siteId)
				}).map(item => {
					const siteId = item.siteId || ''
					const siteType = (item.siteType || item.type || 'cn').toString().toLowerCase()
					return {
						text: item.siteName,
						value: siteId,
						id: item.id,
						longitude: Number(item.longitude || 0),
						latitude: Number(item.latitude || 0),
						siteType
					}
				})
				const siteTypeOptions = createSiteTypeOptions().map(typeItem => ({
					...typeItem,
					children: []
				}))
				siteOptions.forEach(item => {
					const typeOption = siteTypeOptions.find(i => i.value === item.siteType) || siteTypeOptions.find(i => i.value === 'cn')
					if (!typeOption) return
					typeOption.children.push({
						text: item.text,
						value: item.value,
						id: item.id
					})
				})
				const filteredSiteTypeOptions = siteTypeOptions.filter(item => (item.children || []).length > 0)
				const availableSite = siteOptions[0]
				const keepCurrent = siteOptions.find(item => item.value === state.currentSiteId)
				commit('SET_SITE_OPTIONS', siteOptions)
				commit('SET_SITE_TYPE_OPTIONS', filteredSiteTypeOptions)
				commit('SET_CURRENT_SITE_ID', keepCurrent ? keepCurrent.value : (availableSite?.value || ''))
				commit('SET_SITE_LOADED', true)
				return {
					siteOptions,
					siteTypeOptions: filteredSiteTypeOptions,
					siteId: keepCurrent ? keepCurrent.value : (availableSite?.value || '')
				}
			})
		},
		SetCurrentSiteId({ commit }, siteId) {
			commit('SET_CURRENT_SITE_ID', siteId)
		},
		ClearSiteState({ commit }) {
			commit('SET_SITE_OPTIONS', [])
			commit('SET_SITE_TYPE_OPTIONS', [])
			commit('SET_CURRENT_SITE_ID', '')
			commit('SET_SITE_LOADED', false)
		}
	}
}

export default site
