import { getTenantDomain } from './config'

const ACTIVE_BUSINESS_ID_KEY = 'meshkee_customer_active_business_id'
const ACTIVE_BUSINESS_DOMAIN_KEY = 'meshkee_customer_active_business_domain'

export function setActiveBusinessById(businessId: string, domain?: string) {
  localStorage.setItem(ACTIVE_BUSINESS_ID_KEY, businessId)
  localStorage.setItem(ACTIVE_BUSINESS_DOMAIN_KEY, domain ?? getTenantDomain())
}

export function getActiveBusinessId(): string | null {
  return localStorage.getItem(ACTIVE_BUSINESS_ID_KEY)
}

export function getActiveBusinessDomain(): string {
  return localStorage.getItem(ACTIVE_BUSINESS_DOMAIN_KEY) ?? getTenantDomain()
}

export function clearActiveBusiness() {
  localStorage.removeItem(ACTIVE_BUSINESS_ID_KEY)
  localStorage.removeItem(ACTIVE_BUSINESS_DOMAIN_KEY)
}
