import { useUserStore } from '@/store/modules/user';
import { computed, watch } from 'vue';

export const SYS_TENANT_ID = 0;

/** 设置targetTenantId */
export function useTargetTenantIdSetting() {
  let fullKey = '';
  const userStore = useUserStore();

  watch(
    () => [userStore.info.username, userStore.info.tenantCode],
    ([username, tenantCode]) => {
      fullKey = `${tenantCode || ''}_${username}_targetTenantId`;
    },
    { immediate: true },
  );

  function setValue(tenantId: string | string) {
    if (!fullKey) {
      
      return;
    }
    localStorage.setItem(fullKey, String(tenantId));
  }

  function getValue() {
    if (!fullKey) {
      
      return;
    }
    return localStorage.getItem(fullKey);
  }

  // 系统租户
  const sysTenantId = getValue() || userStore.info.tenantId?.toString();
  const isSysTenant = sysTenantId === SYS_TENANT_ID.toString();

  return { setValue, getValue, isSysTenant };
}
