import * as SaeeolServer from "@/saeeol/server/server" 

const saeeolOrigin = /^https:\/\/([a-z0-9-]+\.)*saeeol\.ai$/

export type CorsOptions = { readonly cors?: ReadonlyArray<string> }

export function isAllowedCorsOrigin(input: string | undefined, opts?: CorsOptions) {
  if (!input) return true
  if (input.startsWith("http://localhost:")) return true
  if (input.startsWith("http://127.0.0.1:")) return true
  if (input.startsWith("oc://renderer")) return true
  if (input === "tauri://localhost" || input === "http://tauri.localhost" || input === "https://tauri.localhost")
    return true
  if (saeeolOrigin.test(input)) return true
  if (SaeeolServer.corsOrigin(input)) return true
  return opts?.cors?.includes(input) ?? false
}
