import { AnyConstructable, ConstructableFilter, ConstructableFilterGroup, ConstructableOrderBy } from "./filter.js";
import { FieldTypeOf, ReadOf, ReadShape, SoftrDatasources } from "../action.js";

//#region src/datasource/fields.d.ts
type EmptyChain = {
  isEmpty(): ConstructableFilter;
  isNotEmpty(): ConstructableFilter;
};
type BooleanChain = EmptyChain & {
  is(value: boolean): ConstructableFilter;
  isNot(value: boolean): ConstructableFilter;
};
type NumberChain = EmptyChain & {
  is(value: number): ConstructableFilter;
  isNot(value: number): ConstructableFilter;
  gt(value: number): ConstructableFilter;
  gte(value: number): ConstructableFilter;
  lt(value: number): ConstructableFilter;
  lte(value: number): ConstructableFilter;
  between(start: number, end: number): ConstructableFilter;
};
type ArrayChain = EmptyChain & {
  is(values: Array<string>): ConstructableFilter;
  isOneOf(values: Array<string>): ConstructableFilter;
  isNoneOf(values: Array<string>): ConstructableFilter;
  hasAllOf(values: Array<string>): ConstructableFilter;
};
/**
 * Compares choice ids as plain strings. A select's choices change in Studio after the build,
 * and an id read at runtime from ds.choices has to be a valid filter value.
 */
type ChoiceChain = EmptyChain & {
  is(value: string): ConstructableFilter;
  isNot(value: string): ConstructableFilter;
  isOneOf(values: Array<string>): ConstructableFilter;
  isNoneOf(values: Array<string>): ConstructableFilter;
};
/** What DSv2 filters generically: matched whole or scanned, never against a set. */
type GenericChain = EmptyChain & {
  is(value: string): ConstructableFilter;
  isNot(value: string): ConstructableFilter;
  contains(value: string): ConstructableFilter;
};
type TextChain<TValue extends string> = EmptyChain & {
  is(value: TValue): ConstructableFilter;
  isNot(value: TValue): ConstructableFilter;
  contains(value: string): ConstructableFilter;
  startsWith(value: string): ConstructableFilter;
  endsWith(value: string): ConstructableFilter;
  isOneOf(values: Array<string>): ConstructableFilter;
  isNoneOf(values: Array<string>): ConstructableFilter;
};
type DateChain = EmptyChain & {
  is(value: string): ConstructableFilter;
  isNot(value: string): ConstructableFilter;
  gt(value: string): ConstructableFilter;
  gte(value: string): ConstructableFilter;
  lt(value: string): ConstructableFilter;
  lte(value: string): ConstructableFilter;
  between(start: string, end: string): ConstructableFilter;
  isNotBetween(start: string, end: string): ConstructableFilter;
};
/** Every operator, for a field whose type nothing has generated yet. */
type AnyChain = EmptyChain & {
  is(value: unknown): ConstructableFilter;
  isNot(value: unknown): ConstructableFilter;
  contains(value: string): ConstructableFilter;
  startsWith(value: string): ConstructableFilter;
  endsWith(value: string): ConstructableFilter;
  isOneOf(values: Array<string>): ConstructableFilter;
  isNoneOf(values: Array<string>): ConstructableFilter;
  hasAllOf(values: Array<string>): ConstructableFilter;
  gt(value: number | string): ConstructableFilter;
  gte(value: number | string): ConstructableFilter;
  lt(value: number | string): ConstructableFilter;
  lte(value: number | string): ConstructableFilter;
  between(start: number | string, end: number | string): ConstructableFilter;
  isNotBetween(start: number | string, end: number | string): ConstructableFilter;
};
type TextFieldType = 'SINGLE_LINE_TEXT' | 'LONG_TEXT' | 'URL' | 'EMAIL' | 'PHONE' | 'ADDRESS' | 'RECORD_ID' | 'LOCATION' | 'TIME' | 'BUTTON';
type NumberFieldType = 'NUMBER' | 'PERCENT' | 'CURRENCY' | 'PROGRESS' | 'RATING' | 'DURATION' | 'COUNT' | 'AUTONUMBER';
type DateFieldType = 'DATE' | 'DATETIME' | 'CREATED_AT' | 'UPDATED_AT';
type ChoiceFieldType = 'SELECT' | 'LINKED_RECORD' | 'CREATED_BY' | 'UPDATED_BY';
/** The types with no single value to compare: only whether the field holds one. */
type EmptyFieldType = 'ATTACHMENT' | 'DATE_RANGE';
/**
 * The operators one alias offers, chosen from the type codegen declared for the field rather
 * than from what it reads as: a `DATETIME` and a text field both read as `string`, and only
 * the declared type tells them apart.
 *
 * A field with no declared type lands on the permissive chain rather than on none.
 */
type ChainFor<TFieldType, TValue> = [TFieldType] extends [never] ? AnyChain : TFieldType extends `${infer TBase}[]` ? TBase extends EmptyFieldType ? EmptyChain : ArrayChain : TFieldType extends TextFieldType ? TextChain<TValue & string> : TFieldType extends NumberFieldType ? NumberChain : TFieldType extends 'USER' ? GenericChain : TFieldType extends 'CHECKBOX' ? BooleanChain : TFieldType extends DateFieldType ? DateChain : TFieldType extends ChoiceFieldType ? ChoiceChain : TFieldType extends EmptyFieldType ? EmptyChain : AnyChain;
type Combinators = {
  $and(...members: Array<AnyConstructable>): ConstructableFilterGroup;
  $or(...members: Array<AnyConstructable>): ConstructableFilterGroup;
};
/**
 * What a `where` callback is handed: every alias the action declares, already a chain of the
 * operators that alias accepts, plus `$and` and `$or` to nest them.
 *
 * Keyed off the action's own `fields` map, so an alias resolves to a field id and from there
 * to the declared type. With nothing generated there is no field id to resolve, and an alias
 * the action never declared has to resolve too, so that case is open by name and permissive
 * by operator.
 */
type FieldSet<TShape extends ReadShape> = keyof SoftrDatasources extends never ? Record<string, AnyChain> & Combinators : { [Alias in keyof TShape['fields']]: ChainFor<FieldTypeOf<TShape['datasourceId'], TShape['fields'][Alias]>, NonNullable<ReadOf<TShape['datasourceId']>[TShape['fields'][Alias] & keyof ReadOf<TShape['datasourceId']>]>> } & Combinators;
/** What an `orderBy` callback is handed: two handles per alias and nothing else. */
type SortSet<TRecord> = { [K in keyof TRecord]: {
  readonly asc: ConstructableOrderBy;
  readonly desc: ConstructableOrderBy;
} };
//#endregion
export { AnyChain, ChainFor, DateFieldType, FieldSet, NumberFieldType, SortSet };