import { AnyChain, ChainFor } from "./fields.js";
import { FieldTypeOf, ReadOf, ReadShape, SoftrDatasources } from "../action.js";

//#region src/datasource/enforced-filter.d.ts

/**
 * The operators of one alias, each written as the value its `where` method takes. An operator
 * with no value takes `true`, and a range takes a pair.
 */
type Conditions<TChain> = { [Operator in keyof TChain]?: TChain[Operator] extends ((...args: infer TArgs) => unknown) ? TArgs extends [] ? true : TArgs extends [infer TValue] ? TValue : TArgs extends [infer TStart, infer TEnd] ? [TStart, TEnd] : never : never };
type Junctions<TShape extends ReadShape> = {
  $and?: Array<EnforcedFilter<TShape>>;
  $or?: Array<EnforcedFilter<TShape>>;
};
/**
 * The filter a query enforces on every read. Each key is an alias of the query's `filterFields`,
 * with the same operators as `where`. `$and` and `$or` nest more filters. The keys of one object
 * combine with AND.
 *
 * A read returns `fields`, not `filterFields`. So a field that the visitor must not see can be in
 * `filterFields` only. The datasource refuses a read that filters on a field a data restriction
 * hides, even when it hides the field on some records only.
 *
 * A token stands for a value of the visitor or for a day. Write it in the form that the Studio
 * condition builder stores:
 * - `{USER:::EMAIL}` and `{USER:::EMAIL_DOMAIN}`: the visitor's email and its domain.
 * - `USER:::<field id>`: a field of the visitor's row in the users table. softr.json must declare
 *   it under `user.properties`. For the id of that row, declare a Record ID field.
 * - `PREDEFINED:TODAY`, `PREDEFINED:YESTERDAY`, `PREDEFINED:TOMORROW`,
 *   `FIXED_DATE:DAYS_AGO:<n>` and `FIXED_DATE:DAYS_FROM_NOW:<n>`: a day relative to today.
 *
 * The datasource resolves the tokens on each read. It resolves a day in the app's time zone. A
 * condition on a user token matches no rows for a visitor who is signed out or has no value for
 * it. In an `$or`, the other branches still match.
 *
 * Compare a field of the visitor that can hold several values, such as a link to their teams,
 * with `isOneOf`. With a scalar `is`, a visitor who holds more than one value reads no rows.
 *
 * @example
 * // "7fMhE" is a Record ID field of the users table, declared under `user.properties`.
 * filter: { assignee: { isOneOf: ["USER:::7fMhE"] } }
 *
 * @example
 * // `requesterEmail` is in `filterFields`, so public rows do not show it.
 * filter: {
 *   $or: [
 *     { requesterEmail: { is: "{USER:::EMAIL}" } },
 *     { visibility: { is: "public" }, publishedAt: { lte: "PREDEFINED:TODAY" } },
 *   ],
 * }
 */
type EnforcedFilter<TShape extends ReadShape> = keyof SoftrDatasources extends never ? {
  [key: string]: Conditions<AnyChain> | Array<EnforcedFilter<TShape>>;
} : { [Alias in keyof TShape["fields"]]?: Conditions<ChainFor<FieldTypeOf<TShape["datasourceId"], TShape["fields"][Alias]>, NonNullable<ReadOf<TShape["datasourceId"]>[TShape["fields"][Alias] & keyof ReadOf<TShape["datasourceId"]>]>>> } & Junctions<TShape>;
//#endregion
export { EnforcedFilter };