import { SoftrUserGroupId } from "./auth/user.js";
import { FilterCriteria } from "./datasource/filter.js";
import { EnforcedFilter } from "./datasource/enforced-filter.js";

//#region src/action.d.ts
type ActionOperation = "read" | "create" | "update" | "delete";
type ActionPermissions = {
  userGroupTypes: readonly ["NON_LOGGED_IN_USERS"];
  customGroups?: never;
} | {
  userGroupTypes: readonly ["LOGGED_IN_USERS"] | readonly ["NON_LOGGED_IN_USERS", "LOGGED_IN_USERS"] | readonly ["LOGGED_IN_USERS", "NON_LOGGED_IN_USERS"];
  customGroups?: ReadonlyArray<SoftrUserGroupId>;
};
type Action<TResource> = {
  id: string;
  resource: TResource;
  permissions: ActionPermissions;
};
/**
 * Describes the datasources available in the app and their read, create and update types.
 *
 * This lets datasource IDs and fields in actions be type-checked.
 *
 * @example
 * declare module "@softr/sdk" {
 *   interface SoftrDatasources {
 *     FrkQIVpeTtfJDT: {
 *       name: "Cafes"
 *       primaryField: "9QrOA"
 *       read: CafesRead
 *       create: CafesCreate
 *       update: CafesUpdate
 *       fieldTypes: { "9QrOA": "SINGLE_LINE_TEXT"; "1G3aA": "DATETIME" }
 *     }
 *   }
 * }
 */
interface SoftrDatasources {}
type SoftrDatasourceId = keyof SoftrDatasources extends never ? string : keyof SoftrDatasources & string;
type ReadOf<TDatasource$1> = TDatasource$1 extends keyof SoftrDatasources ? SoftrDatasources[TDatasource$1] extends {
  read: infer TRecord;
} ? TRecord : never : never;
type FieldTypeOf<TDatasource$1, TFieldId> = TDatasource$1 extends keyof SoftrDatasources ? SoftrDatasources[TDatasource$1] extends {
  fieldTypes: infer TTypes;
} ? TFieldId extends keyof TTypes ? TTypes[TFieldId] : never : never : never;
type WriteOf<TDatasource$1, TOperation$1> = TDatasource$1 extends keyof SoftrDatasources ? TOperation$1 extends keyof SoftrDatasources[TDatasource$1] ? SoftrDatasources[TDatasource$1][TOperation$1] : never : never;
type FieldIdOf<TDatasource$1, TOperation$1> = keyof SoftrDatasources extends never ? string : keyof WriteOf<TDatasource$1, TOperation$1> & string;
type ReadFieldIdOf<TDatasource$1> = keyof SoftrDatasources extends never ? string : keyof ReadOf<TDatasource$1> & string;
type ActionShape = {
  id: string;
  datasourceId: SoftrDatasourceId;
  operation: ActionOperation;
  fields?: Record<string, string>;
};
type ReadShape = ActionShape & {
  operation: "read";
  fields: Record<string, string>;
};
type ActionData<TShape extends ActionShape> = keyof SoftrDatasources extends never ? Record<string, unknown> : TShape extends {
  datasourceId: infer TDatasource;
  operation: infer TOperation;
  fields: infer TFields;
} ? { [Alias in keyof TFields]?: WriteOf<TDatasource, TOperation>[TFields[Alias] & keyof WriteOf<TDatasource, TOperation>] } : Record<string, unknown>;
type ActionRecord<TShape extends ActionShape> = keyof SoftrDatasources extends never ? Record<string, unknown> : TShape extends {
  datasourceId: infer TDatasource;
  fields: infer TFields;
} ? { [Alias in keyof TFields]: ReadOf<TDatasource>[TFields[Alias] & keyof ReadOf<TDatasource>] } : Record<string, unknown>;
declare const brand: unique symbol;
type DatasourceResource = {
  type: "DATASOURCE";
  datasourceId: SoftrDatasourceId;
  operation: ActionOperation;
  fields?: Record<string, string>;
  /** The fields that the filter reads, by alias. Only `fields` are read or written. */
  filterFields?: Record<string, string>;
  /** The enforced filter, keyed by alias, with its tokens unresolved. */
  filter?: FilterCriteria;
};
/**
 * A datasource-backed action (query or mutation). The `TShape` brand carries field and
 * operation type info for compile-time checking of `ds.*` calls.
 */
type DatasourceAction<TShape extends ActionShape = ActionShape> = Action<DatasourceResource> & {
  id: TShape["id"];
  readonly [brand]: TShape;
};
type WriteDeclaration<TId extends string, TDatasource$1, TOperation$1, TFields$1> = {
  id: TId;
  datasourceId: TDatasource$1;
  operation: TOperation$1;
  fields: TFields$1;
  permissions: ActionPermissions;
};
type ReadDeclaration<TId extends string, TDatasource$1 extends SoftrDatasourceId, TFields$1 extends Record<string, string>, TFilterFields extends Record<string, string>> = {
  id: TId;
  datasourceId: TDatasource$1;
  fields: TFields$1;
  permissions: ActionPermissions;
  /** The fields that `filter` reads, by alias. A read returns only `fields`. */
  filterFields?: TFilterFields;
  filter?: EnforcedFilter<{
    id: TId;
    datasourceId: TDatasource$1;
    operation: "read";
    fields: NoInfer<TFilterFields>;
  }>;
};
type DeleteDeclaration<TId extends string, TDatasource$1> = {
  id: TId;
  datasourceId: TDatasource$1;
  operation: "delete";
  fields?: never;
  permissions: ActionPermissions;
};
type FilterDeclaration<TId extends string, TDatasource$1 extends SoftrDatasourceId, TFilterFields extends Record<string, string>> = {
  /** The fields that `filter` reads, by alias. A user can write them only if `fields` lists them too. */
  filterFields?: TFilterFields;
  filter?: EnforcedFilter<{
    id: TId;
    datasourceId: TDatasource$1;
    operation: "read";
    fields: NoInfer<TFilterFields>;
  }>;
};
/**
 * Declares a read: which datasource, which fields participate, and who may run it.
 *
 * The argument must be a static object.
 *
 * `filter` limits every read of the query to the rows that match it, and a `where` cannot widen
 * it. Write a filter on the visitor with a user token. A condition on a user token matches no rows
 * for a visitor who is signed out. A filter has at most 4 group levels.
 *
 * The keys of `filter` are aliases of `filterFields`. A read returns only `fields`, so a field
 * that only the filter needs, such as the owner's email, stays hidden. A field that the read
 * returns and the filter reads goes in both.
 *
 * @example
 * const publishedPosts = defineQuery({
 *   id: "publishedPosts",
 *   datasourceId: "8f14e45f-ceea-467a-9d0b-2c1a0e3b5d77",
 *   fields: { title: "V1StGXR8", author: "Z7pQm4Kd", publishedAt: "Xp2dLh6v" },
 *   permissions: { userGroupTypes: ["NON_LOGGED_IN_USERS", "LOGGED_IN_USERS"] },
 * })
 *
 * const { records } = await ds
 *   .list(publishedPosts)
 *   .where((q) => q.publishedAt.lte(today))
 *   .orderBy((q) => q.publishedAt.desc)
 *
 * @example
 * const myRequests = defineQuery({
 *   id: "myRequests",
 *   datasourceId: "8f14e45f-ceea-467a-9d0b-2c1a0e3b5d77",
 *   fields: { title: "V1StGXR8", dueAt: "Xp2dLh6v" },
 *   filterFields: { requesterEmail: "Z7pQm4Kd" },
 *   permissions: { userGroupTypes: ["LOGGED_IN_USERS"] },
 *   filter: { requesterEmail: { is: "{USER:::EMAIL}" }, dueAt: { gte: "FIXED_DATE:DAYS_AGO:7" } },
 * })
 */
declare function defineQuery<const TId extends string, const TDatasource$1 extends SoftrDatasourceId, const TFields$1 extends Record<string, ReadFieldIdOf<TDatasource$1>>, const TFilterFields extends Record<string, ReadFieldIdOf<TDatasource$1>> = Record<never, never>>(definition: ReadDeclaration<TId, TDatasource$1, TFields$1, TFilterFields>): DatasourceAction<{
  id: TId;
  datasourceId: TDatasource$1;
  operation: "read";
  fields: TFields$1;
}>;
/**
 * Declares a write: which datasource, whether it creates, updates or deletes, which fields
 * participate, and who may run it.
 *
 * The argument must be a static object.
 *
 * An update or delete can have a `filter`, with the same operators and tokens as in
 * `defineQuery`. Its keys are aliases of `filterFields`. Before each write, `ds` reads the record
 * through the filter. A record that does not match gives a "not found" error, and nothing is
 * written. A condition on a user token matches no record for a visitor who is signed out.
 *
 * An update cannot write a field that its filter compares with a user token, such as an owner.
 * Otherwise a user could give the record to another user.
 *
 * @example
 * const closeOrder = defineMutation({
 *   id: "closeOrder",
 *   datasourceId: "8f14e45f-ceea-467a-9d0b-2c1a0e3b5d77",
 *   operation: "update",
 *   fields: { status: "V1StGXR8", closedAt: "Z7pQm4Kd" },
 *   permissions: { userGroupTypes: ["LOGGED_IN_USERS"], customGroups: ["grp_staff"] },
 * })
 *
 * await ds.update(closeOrder, { id, data: { status: "closed" } })
 *
 * @example
 * const removeOrder = defineMutation({
 *   id: "removeOrder",
 *   datasourceId: "8f14e45f-ceea-467a-9d0b-2c1a0e3b5d77",
 *   operation: "delete",
 *   permissions: { userGroupTypes: ["LOGGED_IN_USERS"], customGroups: ["grp_staff"] },
 * })
 *
 * await ds.delete(removeOrder, { id })
 *
 * @example
 * const editMyRequest = defineMutation({
 *   id: "editMyRequest",
 *   datasourceId: "8f14e45f-ceea-467a-9d0b-2c1a0e3b5d77",
 *   operation: "update",
 *   fields: { title: "V1StGXR8", details: "Xp2dLh6v" },
 *   filterFields: { requesterEmail: "Z7pQm4Kd" },
 *   filter: { requesterEmail: { is: "{USER:::EMAIL}" } },
 *   permissions: { userGroupTypes: ["LOGGED_IN_USERS"] },
 * })
 */
declare function defineMutation<const TId extends string, const TDatasource$1 extends SoftrDatasourceId, const TFields$1 extends Record<string, FieldIdOf<TDatasource$1, "update">>, const TFilterFields extends Record<string, ReadFieldIdOf<TDatasource$1>> = Record<never, never>>(definition: WriteDeclaration<TId, TDatasource$1, "update", TFields$1> & FilterDeclaration<TId, TDatasource$1, TFilterFields>): DatasourceAction<{
  id: TId;
  datasourceId: TDatasource$1;
  operation: "update";
  fields: TFields$1;
}>;
declare function defineMutation<const TId extends string, const TDatasource$1 extends SoftrDatasourceId, const TOperation$1 extends "create" | "update", const TFields$1 extends Record<string, FieldIdOf<TDatasource$1, TOperation$1>>>(definition: WriteDeclaration<TId, TDatasource$1, TOperation$1, TFields$1> & {
  filterFields?: never;
  filter?: never;
}): DatasourceAction<{
  id: TId;
  datasourceId: TDatasource$1;
  operation: TOperation$1;
  fields: TFields$1;
}>;
declare function defineMutation<const TId extends string, const TDatasource$1 extends SoftrDatasourceId, const TFilterFields extends Record<string, ReadFieldIdOf<TDatasource$1>> = Record<never, never>>(definition: DeleteDeclaration<TId, TDatasource$1> & FilterDeclaration<TId, TDatasource$1, TFilterFields>): DatasourceAction<{
  id: TId;
  datasourceId: TDatasource$1;
  operation: "delete";
}>;
type WorkflowTemplateValue = "{LOGGED_IN_USER:EMAIL}" | "{LOGGED_IN_USER:NAME}" | "{LOGGED_IN_USER:RECORD_ID}" | `{LOGGED_IN_USER:${string}}` | "{CURRENT_DATE_TIME}" | "{CURRENT_DATE}";
type WorkflowBodyValue = WorkflowTemplateValue | (string & {}) | number | boolean | null;
type WorkflowResource = {
  type: "WORKFLOW";
  workflowId: string;
  triggerId: string;
  body?: Record<string, WorkflowBodyValue>;
};
type WorkflowAction<TId extends string = string> = Action<WorkflowResource> & {
  id: TId;
};
type WorkflowDefinition<TId extends string> = {
  id: TId;
  workflowId: string;
  triggerId: string;
  permissions: ActionPermissions;
  body?: Record<string, WorkflowBodyValue>;
};
/**
 * Declares a workflow trigger: which workflow, which trigger, who may run it, and any
 * hardcoded data resolved server-side (e.g. logged-in user fields, current date).
 *
 * Pass the returned object to `workflow.trigger()` at runtime — optionally with additional
 * payload. Values in `body` are resolved by the server and cannot be overridden at runtime.
 *
 * @example
 * const notifyTeam = defineWorkflow({
 *   id: "notifyTeam",
 *   workflowId: "wf_abc123",
 *   triggerId: "tr_xyz789",
 *   permissions: { userGroupTypes: ["LOGGED_IN_USERS"] },
 *   body: { requestedBy: "{LOGGED_IN_USER:EMAIL}", at: "{CURRENT_DATE_TIME}" },
 * });
 *
 * await workflow.trigger(notifyTeam, { subject: "Hello" });
 */
declare function defineWorkflow<const TId extends string>(definition: WorkflowDefinition<TId>): WorkflowAction<TId>;
type IntegrationMethod = "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE";
type IntegrationResource = {
  type: "INTEGRATION";
  integrationId: string;
  method: IntegrationMethod;
  url: string;
};
type IntegrationAction<TId extends string = string> = Action<IntegrationResource> & {
  id: TId;
};
type IntegrationDefinition<TId extends string> = {
  id: TId;
  integrationId: string;
  method: IntegrationMethod;
  url: string;
  permissions: ActionPermissions;
};
/**
 * Declares an integration action: which workspace integration to call, the one HTTP method and the
 * endpoint URL it allows, and who may call it. The proxy denies a call whose method does not match,
 * so declare one action per operation. Use a `{placeholder}` token for a dynamic path segment,
 *
 * Pass the returned object to `integration.createFetcher()` to get a bound fetcher.
 *
 * @example
 * const listCalendarEvents = defineIntegration({
 *   id: "listCalendarEvents",
 *   integrationId: "25d60213-ef4f-4fa0-b094-a46931b2d120",
 *   method: "GET",
 *   url: "https://www.googleapis.com/calendar/v3/calendars/primary/events",
 *   permissions: { userGroupTypes: ["LOGGED_IN_USERS"] },
 * })
 *
 * const listEvents = integration.createFetcher(listCalendarEvents)
 * const res = await listEvents({ query: { timeMin: now, singleEvents: "true" } })
 */
declare function defineIntegration<const TId extends string>(definition: IntegrationDefinition<TId>): IntegrationAction<TId>;
type AiResource = {
  type: "SOFTR_AI";
  model: string;
};
type AiAction<TId extends string = string> = Action<AiResource> & {
  id: TId;
};
type AiDefinition<TId extends string> = {
  id: TId;
  model: string;
  permissions: ActionPermissions;
};
/**
 * Declares an AI action: a model call run by Softr on its own provider keys and billed to the
 * workspace's AI credits, so nothing has to be connected. `model` is the id of a model from
 * Softr's AI catalog, chosen when the app is built: read the catalog with the `ai_list_models`
 * tool and copy the id exactly. An id outside the catalog is refused when the action is saved.
 *
 * Every call spends the workspace's credits, so keep the audience to signed-in users unless a
 * public page really needs the call.
 *
 * Pass the returned object to `ai.generate()` or `ai.stream()` at runtime.
 *
 * @example
 * const summarizeDeal = defineAiAction({
 *   id: "summarizeDeal",
 *   model: "anthropic-claude-sonnet-5-5",
 *   permissions: { userGroupTypes: ["LOGGED_IN_USERS"] },
 * })
 *
 * const answer = await ai.generate(summarizeDeal, { system: "...", prompt: facts })
 */
declare function defineAiAction<const TId extends string>(definition: AiDefinition<TId>): AiAction<TId>;
//#endregion
export { ActionData, ActionPermissions, ActionRecord, ActionShape, AiAction, DatasourceAction, FieldTypeOf, IntegrationAction, ReadOf, ReadShape, SoftrDatasources, WorkflowAction, defineAiAction, defineIntegration, defineMutation, defineQuery, defineWorkflow };