import { defineStore } from 'pinia'
import { getOperatorAllListAPI } from '../api/operator'

interface OperatorItem {
  address: string
  contactPhone: string
  createBy: string
  createTime: string
  isDeleted: boolean
  operatorId: string
  operatorName: string
  updateBy: null | string
  updateTime: null | string
}

/**
 * 操作相关的仓库（后续所有与操作相关的数据都放在这里）
 */
export const useOperatorStore = defineStore(
  'operator',
  () => {
    const operatorList = ref<OperatorItem[]>([])

    /**
     * 获取运营商列表
     * @param isSupportSelect 是否转为为select支持的格式
     */
    async function getOperatorList(isSupportSelect = false) {
      try {
        const _list: OperatorItem[] = await getOperatorAllListAPI()
        if (isSupportSelect) {
          return _list.map(item => ({
            text: item.operatorName,
            value: item.operatorId,
          }))
        }
        return _list
      }
      catch (e) {
        return []
      }
    }

    /**
     *根据operatorId翻译
     */
    function translateOperatorId(operatorId: string, list: OperatorItem[] = []) {
      if (!list || list.length === 0)
        return operatorId

      const operator = list.find(item => item.operatorId === operatorId)
      return operator ? operator.operatorName : operatorId
    }

    return {
      getOperatorList,
      translateOperatorId,
    }
  },
  {
    persist: true,
  },
)
