/* eslint-disable prettier/prettier */
import { computed, nextTick } from "vue";
import { useUserInfo } from "./useUser";
import { useUserStoreHook } from "/@/store/modules/user";



export function useCurrentCompany() {
  const userInfo = useUserInfo();
  const userStore = useUserStoreHook();

  /** 当前用户的公司列表 */  
  const list = computed(() => useUserStoreHook().companyList);
  /** 当前用户选择的公司 */ 
  const current = computed(() => useUserStoreHook().currentCompany || {});
  /** 设置账户的公司列表 */ 
  const setList = (list) => useUserStoreHook().companyList = list

  function setCurrent(value, newValue?: any) {
    const filter: any = ({ companyCode }) => companyCode === value;
    const company = (newValue || list.value).find(filter);
    if (company && company.companyCode) { company.companyNo = company.companyCode }
    /** 超管用户可以查看所有 */ 
    if(userInfo.isSuperUser.value) {
      company.companyName = value?.name || '所有公司'
      company.companyNo = value?.code || ''
    }
    userStore.setCurrentCompany(company);
    return company
  }

  return {
    list,
    setList,
    current,
    setCurrent
  };
}
