import { createEnv } from "@t3-oss/env-nextjs"
import { z } from "zod"

import { getLiveStatus } from "./getLiveStatus"
import { isLangLive } from "./isLangLive"

import type { Lang } from "@/constants/languages"

const _env = createEnv({
  client: {
    NEXT_PUBLIC_NODE_ENV: z.enum(["development", "test", "production"]),
    NEXT_PUBLIC_PORT: z.string().default("3000"),
    NEXT_PUBLIC_SENTRY_ENVIRONMENT: z.string().default("development"),
    NEXT_PUBLIC_SENTRY_CLIENT_SAMPLERATE: z.coerce.number().default(0.001),
    NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS: z
      .string()
      .regex(/^([a-z]{2},)*([a-z]{2}){0,1}$/)
      .transform((val) => {
        return val.split(",")
      })
      .default(""),
    NEXT_PUBLIC_PUBLIC_URL: z.string().optional(),
  },
  emptyStringAsUndefined: true,
  runtimeEnv: {
    NEXT_PUBLIC_NODE_ENV: process.env.NODE_ENV,
    NEXT_PUBLIC_PORT: process.env.NEXT_PUBLIC_PORT,
    NEXT_PUBLIC_SENTRY_ENVIRONMENT: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,
    NEXT_PUBLIC_SENTRY_CLIENT_SAMPLERATE:
      process.env.NEXT_PUBLIC_SENTRY_CLIENT_SAMPLERATE,
    NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS:
      process.env.NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS,
    NEXT_PUBLIC_PUBLIC_URL: process.env.NEXT_PUBLIC_PUBLIC_URL,
  },
})

export const env = {
  ..._env,
  NEW_SITE_LIVE_STATUS: getLiveStatus(_env),
  isLangLive: (lang: Lang) =>
    isLangLive(lang, _env.NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS),
}
