/**
* @since 1.0.0
*/
import * as Equal from "@effect/data/Equal"
import * as Hash from "@effect/data/Hash"
import { NodeInspectSymbol, toJSON, toString } from "@effect/data/Inspectable"
import { EffectTypeId, effectVariance } from "@effect/data/internal/Effect"
import type * as Option from "@effect/data/Option"
import { pipeArguments } from "@effect/data/Pipeable"
const TypeId: Option.TypeId = Symbol.for("@effect/data/Option") as Option.TypeId
const CommonProto = {
[EffectTypeId]: effectVariance,
[TypeId]: {
_A: (_: never) => _
},
[NodeInspectSymbol](this: Option.Option) {
return this.toJSON()
},
pipe() {
return pipeArguments(this, arguments)
},
toString(this: Option.Option) {
return toString(this.toJSON())
}
}
const SomeProto = Object.assign(Object.create(CommonProto), {
_tag: "Some",
[Equal.symbol](this: Option.Some, that: unknown): boolean {
return isOption(that) && isSome(that) && Equal.equals(that.value, this.value)
},
[Hash.symbol](this: Option.Some) {
return Hash.combine(Hash.hash(this._tag))(Hash.hash(this.value))
},
toJSON(this: Option.Some) {
return {
_id: "Option",
_tag: this._tag,
value: toJSON(this.value)
}
}
})
const NoneProto = Object.assign(Object.create(CommonProto), {
_tag: "None",
[Equal.symbol](this: Option.None, that: unknown): boolean {
return isOption(that) && isNone(that)
},
[Hash.symbol](this: Option.None) {
return Hash.combine(Hash.hash(this._tag))
},
toJSON(this: Option.None) {
return {
_id: "Option",
_tag: this._tag
}
}
})
/** @internal */
export const isOption = (input: unknown): input is Option.Option =>
typeof input === "object" && input != null && TypeId in input
/** @internal */
export const isNone = (fa: Option.Option): fa is Option.None => fa._tag === "None"
/** @internal */
export const isSome = (fa: Option.Option): fa is Option.Some => fa._tag === "Some"
/** @internal */
export const none: Option.Option = Object.create(NoneProto)
/** @internal */
export const some = (value: A): Option.Option => {
const a = Object.create(SomeProto)
a.value = value
return a
}