export * from './wxLogin'
export * from './initWXConfig'
import * as ww from '@wecom/jssdk'

const DEFAULT_SHARE_TITLE = '企业文化'
const DEFAULT_SHARE_DESC = '点击查看详情'
const DEFAULT_SHARE_IMAGE =
  'https://res.mail.qq.com/node/ww/wwmng/style/images/index_share_logo$13c64306.png'

interface IShareWxOption {
  title: string
  link?: string
  desc?: string
  imgUrl?: string
}

export async function wxShare(option: IShareWxOption) {
  await ww.shareAppMessage({
    title: option.title || DEFAULT_SHARE_TITLE,
    desc: option.desc || DEFAULT_SHARE_DESC,
    link: option.link || location.href,
    imgUrl: option.imgUrl || DEFAULT_SHARE_IMAGE,
  })
}

interface ISelectDepOrUser {
  err_msg: string
  result: IResult
}

interface IResult {
  userList: ISelectUser[]
  departmentList: ISelectDept[]
}

export interface ISelectUser {
  id: string
  name: string
  avatar: string
}

export interface ISelectDept {
  id: string
  name: string
}

export function selectDepOrUser(wxOption = {}): Promise<IResult> {
  const defaultOption = {
    fromDepartmentId: -1,
    mode: 'single',
    type: ['user', 'department'],
  }
  const option = {
    ...defaultOption,
    ...wxOption,
  }
  return new Promise((resolve, reject) => {
    ww.invoke('selectEnterpriseContact', option, function (res: ISelectDepOrUser) {
      if (res.err_msg == 'selectEnterpriseContact:ok') {
        resolve(res.result)
      } else {
        reject(res)
      }
    })
  })
}
