import { isFunction, isPlainObject } from '../../internal' import { looseCurryN } from '../../functional' import { Mutation, isMutation, Data, isData, isAtomLike, DEFAULT_DATA_OPTIONS, DEFAULT_MUTATION_OPTIONS } from '../atoms' import { isReplayMediator, replayWithLatest } from '../mediators' import type { MutatorOriginalTransformationUnion } from '../particles' import type { DataOptions, MutationOptions } from '../atoms' import type { ReplayDataMediator, ReplayMutationMediator } from '../mediators' export type MutationToData = Target extends Mutation ? Data : Target extends ReplayMutationMediator ? ReplayDataMediator : never export function mutationToDataS

( mutationLike: Mutation, options?: DataOptions ): Data export function mutationToDataS

( mutationLike: ReplayMutationMediator, options?: DataOptions ): ReplayDataMediator export function mutationToDataS

( mutationLike: Mutation | ReplayMutationMediator, options: DataOptions = DEFAULT_DATA_OPTIONS ): Data | ReplayDataMediator { if (!isMutation(mutationLike)) { throw (new TypeError('"mutation" is expected to be type of "MutationLike".')) } if (!isPlainObject(options)) { throw (new TypeError('"options" is expected to be type of "PlainObject".')) } const preparedOptions = { ...DEFAULT_DATA_OPTIONS, ...options } const finalData = Data.empty(preparedOptions) // mutation -> finalData finalData.observe(mutationLike) if (isReplayMediator(mutationLike)) { return replayWithLatest(1, finalData) } else { return finalData } } /** * Curried version of {@link mutationToDataS}. */ export const mutationToDataS_ = looseCurryN(1, mutationToDataS) export function mutationToData

( transformation: MutatorOriginalTransformationUnion, mutationLike: Mutation, options?: MutationOptions ): Data export function mutationToData

( transformation: MutatorOriginalTransformationUnion, mutationLike: ReplayMutationMediator, options?: MutationOptions ): ReplayDataMediator export function mutationToData

( transformation: MutatorOriginalTransformationUnion, mutationLike: Mutation | ReplayMutationMediator, options: MutationOptions = DEFAULT_MUTATION_OPTIONS ): Data | ReplayDataMediator { if (!isFunction(transformation)) { throw (new TypeError('"transform" is expected to be type of "Function".')) } if (!isMutation(mutationLike)) { throw (new TypeError('"mutation" is expected to be type of "Mutation".')) } if (!isPlainObject(options)) { throw (new TypeError('"options" is expected to be type of "PlainObject".')) } const preparedOptions = { ...DEFAULT_MUTATION_OPTIONS, ...options } const _data = Data.empty() const _mutation = Mutation.ofLift(transformation, preparedOptions) const _data2 = Data.empty() // mutation -> _data -> _mutation -> _data2 _data2.observe(_mutation) _mutation.observe(_data) _data.observe(mutationLike) if (isReplayMediator(mutationLike)) { return replayWithLatest(1, _data2) } else { return _data2 } } /** * Curried version of {@link mutationToData}. */ export const mutationToData_ = looseCurryN(2, mutationToData) export function dataToData ( transformation: MutatorOriginalTransformationUnion, dataLike: Data, options?: MutationOptions ): Data export function dataToData ( transformation: MutatorOriginalTransformationUnion, dataLike: ReplayDataMediator, options?: MutationOptions ): ReplayDataMediator export function dataToData ( transformation: MutatorOriginalTransformationUnion, dataLike: Data | ReplayDataMediator, options: MutationOptions = DEFAULT_MUTATION_OPTIONS ): Data | ReplayDataMediator { if (!isFunction(transformation)) { throw (new TypeError('"transform" is expected to be type of "Function".')) } if (!isData(dataLike)) { throw (new TypeError('"data" is expected to be type of "DataLike".')) } if (!isPlainObject(options)) { throw (new TypeError('"options" is expected to be type of "PlainObject".')) } const preparedOptions = { ...DEFAULT_MUTATION_OPTIONS, ...options } const _mutation = Mutation.ofLift(transformation, preparedOptions) const _data = Data.empty() // data -> _mutation -> _data _data.observe(_mutation) _mutation.observe(dataLike) if (isReplayMediator(dataLike)) { return replayWithLatest(1, _data) } else { return _data } } /** * Curried version of {@link dataToData}. */ export const dataToData_ = looseCurryN(2, dataToData) export function atomToData

( transformation: MutatorOriginalTransformationUnion, mutationLike: Mutation, options?: MutationOptions ): Data export function atomToData

( transformation: MutatorOriginalTransformationUnion, mutationLike: ReplayMutationMediator, options?: MutationOptions ): ReplayDataMediator export function atomToData ( transformation: MutatorOriginalTransformationUnion, dataLike: Data, options?: MutationOptions ): Data export function atomToData ( transformation: MutatorOriginalTransformationUnion, dataLike: ReplayDataMediator, options?: MutationOptions ): ReplayDataMediator export function atomToData (transformation: any, atomLike: any, options: any = {}): any { if (!isFunction(transformation)) { throw (new TypeError('"transform" is expected to be type of "Function".')) } if (!isAtomLike(atomLike)) { throw (new TypeError('"atom" is expected to be type of "AtomLike".')) } if (isMutation(atomLike)) { return mutationToData(transformation, atomLike, options) } else if (isData(atomLike)) { return dataToData(transformation, atomLike, options) } else { throw (new TypeError('Unrecognized type of "Atom" received in atomToData, expected "AtomLike".')) } } /** * Curried version of {@link atomToData}. */ export const atomToData_ = looseCurryN(2, atomToData) export type DataToMutation = Target extends Data ? Mutation : Target extends ReplayDataMediator ? ReplayMutationMediator : never export function dataToMutationS ( dataLike: Data, options?: MutationOptions ): Mutation export function dataToMutationS ( dataLike: ReplayDataMediator, options?: MutationOptions ): ReplayMutationMediator export function dataToMutationS ( dataLike: Data | ReplayDataMediator, options: MutationOptions = DEFAULT_MUTATION_OPTIONS ): Mutation | ReplayMutationMediator { if (!isData(dataLike)) { throw (new TypeError('"data" is expected to be type of "DataLike".')) } if (!isPlainObject(options)) { throw (new TypeError('"options" is expected to be type of "PlainObject".')) } // Passby anything received const _mutation = Mutation.ofLiftBoth(prev => prev as any, options) // data -> _mutation _mutation.observe(dataLike) if (isReplayMediator(dataLike)) { return replayWithLatest(1, _mutation) } else { return _mutation } } /** * Curried version of {@link dataToMutationS}. */ export const dataToMutationS_ = looseCurryN(1, dataToMutationS) export function dataToMutation ( transformation: MutatorOriginalTransformationUnion, dataLike: Data, options?: MutationOptions ): Mutation export function dataToMutation ( transformation: MutatorOriginalTransformationUnion, dataLike: ReplayDataMediator, options?: MutationOptions ): ReplayMutationMediator export function dataToMutation ( transformation: MutatorOriginalTransformationUnion, dataLike: Data | ReplayDataMediator, options: MutationOptions = DEFAULT_MUTATION_OPTIONS ): Mutation | ReplayMutationMediator { if (!isFunction(transformation)) { throw (new TypeError('"transform" is expected to be type of "Function".')) } if (!isData(dataLike)) { throw (new TypeError('"data" is expected to be type of "DataLike".')) } if (!isPlainObject(options)) { throw (new TypeError('"options" is expected to be type of "PlainObject".')) } const preparedOptions = { ...DEFAULT_MUTATION_OPTIONS, ...options } const _mutation = Mutation.ofLift(transformation, preparedOptions) // data -> _mutation _mutation.observe(dataLike) if (isReplayMediator(dataLike)) { return replayWithLatest(1, _mutation) } else { return _mutation } } /** * Curried version of {@link dataToMutation}. */ export const dataToMutation_ = looseCurryN(2, dataToMutation) export function mutationToMutation

( transformation: MutatorOriginalTransformationUnion, mutationLike: Mutation, options?: MutationOptions ): Mutation export function mutationToMutation

( transformation: MutatorOriginalTransformationUnion, mutationLike: ReplayMutationMediator, options?: MutationOptions ): ReplayMutationMediator export function mutationToMutation

( transformation: MutatorOriginalTransformationUnion, mutationLike: Mutation | ReplayMutationMediator, options: MutationOptions = DEFAULT_MUTATION_OPTIONS ): Mutation | ReplayMutationMediator { if (!isFunction(transformation)) { throw (new TypeError('"transform" is expected to be type of "Function".')) } if (!isMutation(mutationLike)) { throw (new TypeError('"mutation" is expected to be type of "MutationLike".')) } if (!isPlainObject(options)) { throw (new TypeError('"options" is expected to be type of "PlainObject".')) } const preparedOptions = { ...DEFAULT_MUTATION_OPTIONS, ...options } const _data = Data.empty() const _mutation = Mutation.ofLift(transformation, preparedOptions) // mutation -> _data -> _mutation _mutation.observe(_data) _data.observe(mutationLike) if (isReplayMediator(mutationLike)) { return replayWithLatest(1, _mutation) } else { return _mutation } } /** * Curried version of {@link mutationToMutation}. */ export const mutationToMutation_ = looseCurryN(2, mutationToMutation) export function atomToMutation ( transformation: MutatorOriginalTransformationUnion, dataLike: Data, options?: MutationOptions ): Mutation export function atomToMutation ( transformation: MutatorOriginalTransformationUnion, dataLike: ReplayDataMediator, options?: MutationOptions ): ReplayMutationMediator export function atomToMutation

( transformation: MutatorOriginalTransformationUnion, mutationLike: Mutation, options?: MutationOptions ): Mutation export function atomToMutation

( transformation: MutatorOriginalTransformationUnion, mutationLike: ReplayMutationMediator, options?: MutationOptions ): ReplayMutationMediator export function atomToMutation (transform: any, atomLike: any, options: any = {}): any { if (!isFunction(transform)) { throw (new TypeError('"transform" is expected to be type of "Function".')) } if (!isAtomLike(atomLike)) { throw (new TypeError('"atom" is expected to be type of "AtomLike".')) } if (isMutation(atomLike)) { return mutationToMutation(transform, atomLike, options) } if (isData(atomLike)) { return dataToMutation(transform, atomLike, options) } throw (new TypeError('Unrecognized type of "Atom" received in atomToMutation, expected "AtomLike".')) } /** * Curried version of {@link atomToMutation}. */ export const atomToMutation_ = looseCurryN(2, atomToMutation)