/*--------------------------------------------------------------------------

TypeBox

The MIT License (MIT)

Copyright (c) 2017-2026 Haydn Paterson 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

---------------------------------------------------------------------------*/

// deno-fmt-ignore-file

import { Guard } from '../../../guard/index.ts'
import { type TResult, Ok, Fail, IsOk } from './try_result.ts'
import { TryBigInt } from './try_bigint.ts'

// ------------------------------------------------------------------
// BigInt
// ------------------------------------------------------------------
const maxBigInt = BigInt(Number.MAX_SAFE_INTEGER)
const minBigInt = BigInt(Number.MIN_SAFE_INTEGER)
function FromBigInt(value: bigint): TResult<number> {
  return (value <= maxBigInt && value >= minBigInt) ? Ok(Number(value)) : Fail()
}
function FromBoolean(value: boolean): TResult<number> {
  return Ok(value ? 1 : 0)
}
// ------------------------------------------------------------------
// String
// ------------------------------------------------------------------
function FromString(value: string): TResult<number> {
  const coerced = +value
  if (Guard.IsNumber(coerced)) return Ok(coerced)
  const lowercase = value.toLowerCase()
  if (Guard.IsEqual(lowercase, 'false')) return Ok(0)
  if (Guard.IsEqual(lowercase, 'true')) return Ok(1)
  const result = TryBigInt(value)
  if (IsOk(result)) return (result.value <= maxBigInt && result.value >= minBigInt) ? Ok(Number(result.value)) : Fail()
  return Fail()
}
// ------------------------------------------------------------------
// Try
// ------------------------------------------------------------------
export function TryNumber(value: unknown): TResult<number> {
  return (
    Guard.IsBigInt(value) ? FromBigInt(value) :
    Guard.IsBoolean(value) ? FromBoolean(value) :
    Guard.IsNumber(value) ? Ok(value) :
    Guard.IsNull(value) ? Ok(0) :
    Guard.IsString(value) ? FromString(value) :
    Guard.IsUndefined(value) ? Ok(0) :
    Fail()
  )
}