(module main)

(import std.env (get get_result get_option has get_or get_i32_result get_i32_option get_i32_or_zero get_i32_or get_u32_result get_u32_option get_u32_or_zero get_u32_or get_i64_result get_i64_option get_i64_or_zero get_i64_or get_u64_result get_u64_option get_u64_or_zero get_u64_or get_f64_result get_f64_option get_f64_or_zero get_f64_or get_bool_result get_bool_option get_bool_or_false get_bool_or))

(fn missing_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_UNLIKELY_MISSING")

(fn present_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_PRESENT")

(fn present_env_value () -> string
  "glagol-std-import-env-alpha-value")

(fn present_i32_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_PRESENT_I32")

(fn present_i64_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_PRESENT_I64")

(fn present_u32_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_PRESENT_U32")

(fn present_u64_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_PRESENT_U64")

(fn present_f64_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_PRESENT_F64")

(fn present_bool_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_PRESENT_BOOL")

(fn invalid_env_name () -> string
  "GLAGOL_STD_IMPORT_ENV_ALPHA_INVALID")

(fn imported_env_missing_get_empty () -> bool
  (= (get (missing_env_name)) ""))

(fn imported_env_missing_result_err_one () -> bool
  (match (get_result (missing_env_name))
    ((ok payload)
      false)
    ((err code)
      (= code 1))))

(fn imported_env_has_missing_false () -> bool
  (if (has (missing_env_name))
    false
    true))

(fn imported_env_get_or_missing_fallback () -> bool
  (= (get_or (missing_env_name) "fallback") "fallback"))

(fn imported_env_get_option_missing_none () -> bool
  (match (get_option (missing_env_name))
    ((some payload)
      false)
    ((none)
      true)))

(fn imported_env_has_present_true () -> bool
  (has (present_env_name)))

(fn imported_env_get_or_present_value () -> bool
  (if (= (get_or (present_env_name) "fallback") (present_env_value))
    (= (get (present_env_name)) (present_env_value))
    false))

(fn imported_env_get_option_present_some () -> bool
  (match (get_option (present_env_name))
    ((some payload)
      (= payload (present_env_value)))
    ((none)
      false)))

(fn imported_env_get_i32_result_present () -> bool
  (match (get_i32_result (present_i32_env_name))
    ((ok value)
      (= value 42))
    ((err code)
      false)))

(fn imported_env_get_i32_or_zero_invalid () -> bool
  (= (get_i32_or_zero (invalid_env_name)) 0))

(fn imported_env_get_u32_result_present () -> bool
  (match (get_u32_result (present_u32_env_name))
    ((ok value)
      (= value 42u32))
    ((err code)
      false)))

(fn imported_env_get_u32_or_zero_invalid () -> bool
  (= (get_u32_or_zero (invalid_env_name)) 0u32))

(fn imported_env_get_i64_result_present () -> bool
  (match (get_i64_result (present_i64_env_name))
    ((ok value)
      (= value 42000000000i64))
    ((err code)
      false)))

(fn imported_env_get_i64_or_zero_missing () -> bool
  (= (get_i64_or_zero (missing_env_name)) 0i64))

(fn imported_env_get_u64_result_present () -> bool
  (match (get_u64_result (present_u64_env_name))
    ((ok value)
      (= value 4294967296u64))
    ((err code)
      false)))

(fn imported_env_get_u64_or_zero_missing () -> bool
  (= (get_u64_or_zero (missing_env_name)) 0u64))

(fn imported_env_get_f64_result_present () -> bool
  (match (get_f64_result (present_f64_env_name))
    ((ok value)
      (= value 42.5))
    ((err code)
      false)))

(fn imported_env_get_f64_or_zero_invalid () -> bool
  (= (get_f64_or_zero (invalid_env_name)) 0.0))

(fn imported_env_get_bool_result_present () -> bool
  (match (get_bool_result (present_bool_env_name))
    ((ok value)
      value)
    ((err code)
      false)))

(fn imported_env_get_bool_or_false_invalid () -> bool
  (if (get_bool_or_false (invalid_env_name))
    false
    true))

(fn imported_env_typed_options_ok () -> bool
  (let i32_present bool (match (get_i32_option (present_i32_env_name))
    ((some payload)
      (= payload 42))
    ((none)
      false)))
  (let i32_invalid bool (match (get_i32_option (invalid_env_name))
    ((some payload)
      false)
    ((none)
      true)))
  (let u32_present bool (match (get_u32_option (present_u32_env_name))
    ((some payload)
      (= payload 42u32))
    ((none)
      false)))
  (let u32_invalid bool (match (get_u32_option (invalid_env_name))
    ((some payload)
      false)
    ((none)
      true)))
  (let i64_present bool (match (get_i64_option (present_i64_env_name))
    ((some payload)
      (= payload 42000000000i64))
    ((none)
      false)))
  (let i64_missing bool (match (get_i64_option (missing_env_name))
    ((some payload)
      false)
    ((none)
      true)))
  (let u64_present bool (match (get_u64_option (present_u64_env_name))
    ((some payload)
      (= payload 4294967296u64))
    ((none)
      false)))
  (let u64_missing bool (match (get_u64_option (missing_env_name))
    ((some payload)
      false)
    ((none)
      true)))
  (let f64_present bool (match (get_f64_option (present_f64_env_name))
    ((some payload)
      (= payload 42.5))
    ((none)
      false)))
  (let f64_invalid bool (match (get_f64_option (invalid_env_name))
    ((some payload)
      false)
    ((none)
      true)))
  (let bool_present bool (match (get_bool_option (present_bool_env_name))
    ((some payload)
      payload)
    ((none)
      false)))
  (let bool_invalid bool (match (get_bool_option (invalid_env_name))
    ((some payload)
      false)
    ((none)
      true)))
  (if i32_present
    (if i32_invalid
      (if u32_present
        (if u32_invalid
          (if i64_present
            (if i64_missing
              (if u64_present
                (if u64_missing
                  (if f64_present
                    (if f64_invalid
                      (if bool_present
                        bool_invalid
                        false)
                      false)
                    false)
                  false)
                false)
              false)
            false)
          false)
        false)
      false)
    false))

(fn imported_env_typed_custom_fallbacks_ok () -> bool
  (if (= (get_i32_or (present_i32_env_name) 7) 42)
    (if (= (get_i32_or (invalid_env_name) 7) 7)
      (if (= (get_u32_or (present_u32_env_name) 7u32) 42u32)
        (if (= (get_u32_or (invalid_env_name) 7u32) 7u32)
          (if (= (get_i64_or (present_i64_env_name) 9i64) 42000000000i64)
            (if (= (get_i64_or (invalid_env_name) 9i64) 9i64)
              (if (= (get_u64_or (present_u64_env_name) 9u64) 4294967296u64)
                (if (= (get_u64_or (invalid_env_name) 9u64) 9u64)
                  (if (= (get_f64_or (present_f64_env_name) 1.5) 42.5)
                    (if (= (get_f64_or (invalid_env_name) 1.5) 1.5)
                      (if (get_bool_or (present_bool_env_name) false)
                        (get_bool_or (invalid_env_name) true)
                        false)
                      false)
                    false)
                  false)
                false)
              false)
            false)
          false)
        false)
      false)
    false))

(fn imported_env_facade_ok () -> bool
  (if (imported_env_missing_get_empty)
    (if (imported_env_missing_result_err_one)
      (if (imported_env_has_missing_false)
        (if (imported_env_get_or_missing_fallback)
          (if (imported_env_get_option_missing_none)
            (if (imported_env_has_present_true)
              (if (imported_env_get_or_present_value)
                (if (imported_env_get_option_present_some)
                  (if (imported_env_get_i32_result_present)
                    (if (imported_env_get_i32_or_zero_invalid)
                      (if (imported_env_get_u32_result_present)
                        (if (imported_env_get_u32_or_zero_invalid)
                          (if (imported_env_get_i64_result_present)
                            (if (imported_env_get_i64_or_zero_missing)
                              (if (imported_env_get_u64_result_present)
                                (if (imported_env_get_u64_or_zero_missing)
                                  (if (imported_env_get_f64_result_present)
                                    (if (imported_env_get_f64_or_zero_invalid)
                                      (if (imported_env_get_bool_result_present)
                                        (if (imported_env_get_bool_or_false_invalid)
                                          (if (imported_env_typed_options_ok)
                                            (imported_env_typed_custom_fallbacks_ok)
                                            false)
                                          false)
                                        false)
                                      false)
                                    false)
                                  false)
                                false)
                              false)
                            false)
                          false)
                        false)
                      false)
                    false)
                  false)
                false)
              false)
            false)
          false)
        false)
      false)
    false))

(fn main () -> i32
  (if (imported_env_facade_ok)
    42
    1))

(test "explicit std env get missing facade"
  (imported_env_missing_get_empty))

(test "explicit std env get result missing facade"
  (imported_env_missing_result_err_one))

(test "explicit std env has missing facade"
  (imported_env_has_missing_false))

(test "explicit std env get or missing facade"
  (imported_env_get_or_missing_fallback))

(test "explicit std env get option missing facade"
  (imported_env_get_option_missing_none))

(test "explicit std env has present facade"
  (imported_env_has_present_true))

(test "explicit std env get or present facade"
  (imported_env_get_or_present_value))

(test "explicit std env get option present facade"
  (imported_env_get_option_present_some))

(test "explicit std env get i32 result present facade"
  (imported_env_get_i32_result_present))

(test "explicit std env get i32 or zero invalid facade"
  (imported_env_get_i32_or_zero_invalid))

(test "explicit std env get u32 result present facade"
  (imported_env_get_u32_result_present))

(test "explicit std env get u32 or zero invalid facade"
  (imported_env_get_u32_or_zero_invalid))

(test "explicit std env get i64 result present facade"
  (imported_env_get_i64_result_present))

(test "explicit std env get i64 or zero missing facade"
  (imported_env_get_i64_or_zero_missing))

(test "explicit std env get u64 result present facade"
  (imported_env_get_u64_result_present))

(test "explicit std env get u64 or zero missing facade"
  (imported_env_get_u64_or_zero_missing))

(test "explicit std env get f64 result present facade"
  (imported_env_get_f64_result_present))

(test "explicit std env get f64 or zero invalid facade"
  (imported_env_get_f64_or_zero_invalid))

(test "explicit std env get bool result present facade"
  (imported_env_get_bool_result_present))

(test "explicit std env get bool or false invalid facade"
  (imported_env_get_bool_or_false_invalid))

(test "explicit std env typed option facade"
  (imported_env_typed_options_ok))

(test "explicit std env typed custom fallback facade"
  (imported_env_typed_custom_fallbacks_ok))

(test "explicit std env facade all"
  (= (main) 42))
