import {
  CUSTOMER_SUBDOMAIN_PREFIX,
  getBaseDomainFromHost,
  getCustomerDashboardHost,
  isCustomerDashboardHost,
} from '@meshkee/dashboard-core'

export { CUSTOMER_SUBDOMAIN_PREFIX }

/** Base tenant domain without customer. prefix (e.g. sanihome.ir). */
export function getBaseBusinessDomain(hostname = window.location.hostname): string {
  return getBaseDomainFromHost(hostname, import.meta.env.VITE_BUSINESS_DOMAIN)
}

/** Expected customer dashboard host (e.g. customer.sanihome.ir). */
export function getCustomerDashboardHostForApp(baseDomain?: string): string {
  return getCustomerDashboardHost(baseDomain ?? getBaseBusinessDomain())
}

export function isAllowedCustomerHost(hostname = window.location.hostname): boolean {
  const host = hostname.toLowerCase().trim()

  if (host === 'localhost' || host === '127.0.0.1') {
    return true
  }

  return isCustomerDashboardHost(host)
}

/** Domain sent to tenant resolution and customer registration APIs. */
export function getTenantDomain(): string {
  return getBaseBusinessDomain()
}
