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

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.

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

import { Arguments, Settings } from '../../system/index.ts'
import type { TProperties, TSchema } from '../../type/index.ts'
import { FromType } from './from_type.ts'

import { UnionPrioritySort } from '../shared/union_priority_sort.ts'

/**
 * Cleans a value by removing non-evaluated properties and elements as derived from the provided type.
 * This function returns unknown so callers should Check the return value before use. This function
 * mutates the provided value. If mutation is not wanted, you should Clone the value before passing
 * to this function.
 */
export function Clean(type: TSchema, value: unknown): unknown
/**
 * Cleans a value by removing non-evaluated properties and elements as derived from the provided type.
 * This function returns unknown so callers should Check the return value before use. This function
 * mutates the provided value. If mutation is not wanted, you should Clone the value before passing
 * to this function.
 */
export function Clean(context: TProperties, type: TSchema, value: unknown): unknown
/**
 * Cleans a value by removing non-evaluated properties and elements as derived from the provided type.
 * This function returns unknown so callers should Check the return value before use. This function
 * mutates the provided value. If mutation is not wanted, you should Clone the value before passing
 * to this function.
 */
export function Clean(...args: unknown[]): unknown {
  const [context, type, value] = Arguments.Match<[TProperties, TSchema, unknown]>(args, {
    3: (context, type, value) => [context, type, value],
    2: (type, value) => [{}, type, value]
  })
  const sorted = Settings.Get().unionPrioritySort ? UnionPrioritySort(type) : type
  return FromType(context, sorted, value)
}
