/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */

import { ReadonlySet } from "@effect-app/core/Prelude"
import { arbitrary, type PositiveBrand, positiveExcludeZero, type PositiveExcludeZeroBrand } from "@effect-app/prelude/schema"
import { brand, positive } from "@effect-app/prelude/schema"
import { number } from "effect/Equivalence"
import { Arbitrary } from "fast-check"

export const PositiveNumber = positive("float")(number).pipe(brand<PositiveNumber>())
export type PositiveNumber = number & PositiveBrand

export const PositiveNumberZeroExclusive = positiveExcludeZero("float")(number).pipe(brand<PositiveNumber>())
export type PositiveNumberZeroExclusive = number & PositiveExcludeZeroBrand

export interface CentimeterBrand extends PositiveBrand {
  readonly CentimeterBrand: unique symbol
}

export type Centimeter = number & CentimeterBrand

export const Centimeter = positive("float")(number).pipe(brand<Centimeter>())

export interface KilogramBrand extends PositiveBrand {
  readonly KilogramBrand: unique symbol
}

export type Kilogram = number & KilogramBrand

export const Kilogram = positive("float")(number).pipe(brand<Kilogram>())

// Limit arbitrary collections to generate a max of 6 entries
// TODO: dictionary, map
const MAX_LENGTH = 6

export function nonEmptyArray<To, ConstructorInput, From, Api>(
  self: Schema<unknown, To, ConstructorInput, From, Api>
) {
  const arbitrarySelf = Arbitrary.for(self)

  return nonEmptyArrayOriginal(self).pipe(
    arbitrary(
      (_) =>
        _.array(arbitrarySelf(_), {
          minLength: 1,
          maxLength: MAX_LENGTH
        }) as any as Arbitrary.Arbitrary<NonEmptyReadonlyArray<To>>
    )
  )
}

export function array<To, ConstructorInput, From, Api>(
  self: Schema<unknown, To, ConstructorInput, From, Api>
) {
  const arbitrarySelf = Arbitrary.for(self)

  return arrayOriginal(self).pipe(
    arbitrary(
      (_) =>
        _.array(arbitrarySelf(_), {
          maxLength: MAX_LENGTH
        }) as any as Arbitrary.Arbitrary<Array<To>>
    )
  )
}

export function set<To, ConstructorInput, From, Api>(
  self: Schema<unknown, To, ConstructorInput, From, Api>,
  ord: Order<To>,
  eq: Equivalence<To>
) {
  const arbitrarySelf = Arbitrary.for(self)
  return setOriginal(self, ord, eq).pipe(
    arbitrary((_) => _.uniqueArray(arbitrarySelf(_), { maxLength: MAX_LENGTH }).map(ReadonlySet.fromArray(eq)))
  )
}
