import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
import { AnySQLiteTable, AnySQLiteColumn, BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core';
import { O as OrganizationRole, f as WorkspaceRole } from './roles-BC1n4t37.js';

/** A product table referenced by FK — only the `id` column is touched. */
type TeamParentTable = AnySQLiteTable & {
    id: AnySQLiteColumn;
};
interface CreateTeamTablesOptions {
    /** The product's user table — org/member rows reference `userTable.id`. */
    userTable: TeamParentTable;
    /** The product's workspace table — workspace members reference `workspaceTable.id`. */
    workspaceTable: TeamParentTable;
}
declare function createTeamTables(opts: CreateTeamTablesOptions): {
    organizations: drizzle_orm_sqlite_core.SQLiteTableWithColumns<{
        name: "organization";
        schema: undefined;
        columns: {
            id: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "id";
                tableName: "organization";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: true;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            name: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "name";
                tableName: "organization";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            slug: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "slug";
                tableName: "organization";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            kind: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "kind";
                tableName: "organization";
                dataType: "string";
                columnType: "SQLiteText";
                data: "personal" | "team";
                driverParam: string;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: ["personal", "team"];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            createdBy: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "created_by";
                tableName: "organization";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "created_at";
                tableName: "organization";
                dataType: "date";
                columnType: "SQLiteTimestamp";
                data: Date;
                driverParam: number;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: undefined;
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {}>;
            updatedAt: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "updated_at";
                tableName: "organization";
                dataType: "date";
                columnType: "SQLiteTimestamp";
                data: Date;
                driverParam: number;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: undefined;
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {}>;
        };
        dialect: "sqlite";
    }>;
    organizationMembers: drizzle_orm_sqlite_core.SQLiteTableWithColumns<{
        name: "organization_member";
        schema: undefined;
        columns: {
            id: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "id";
                tableName: "organization_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: true;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            organizationId: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "organization_id";
                tableName: "organization_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            userId: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "user_id";
                tableName: "organization_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            role: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "role";
                tableName: "organization_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: "admin" | "owner" | "member" | "billing";
                driverParam: string;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: ["owner", "admin", "member", "billing"];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "created_at";
                tableName: "organization_member";
                dataType: "date";
                columnType: "SQLiteTimestamp";
                data: Date;
                driverParam: number;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: undefined;
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {}>;
            updatedAt: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "updated_at";
                tableName: "organization_member";
                dataType: "date";
                columnType: "SQLiteTimestamp";
                data: Date;
                driverParam: number;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: undefined;
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {}>;
        };
        dialect: "sqlite";
    }>;
    workspaceMembers: drizzle_orm_sqlite_core.SQLiteTableWithColumns<{
        name: "workspace_member";
        schema: undefined;
        columns: {
            id: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "id";
                tableName: "workspace_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: true;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            workspaceId: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "workspace_id";
                tableName: "workspace_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: true;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            organizationMemberId: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "organization_member_id";
                tableName: "workspace_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: false;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            userId: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "user_id";
                tableName: "workspace_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: false;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            role: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "role";
                tableName: "workspace_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: "viewer" | "editor" | "admin" | "owner";
                driverParam: string;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: ["owner", "admin", "editor", "viewer"];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            invitedBy: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "invited_by";
                tableName: "workspace_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: false;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            inviteEmail: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "invite_email";
                tableName: "workspace_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: false;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            inviteToken: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "invite_token";
                tableName: "workspace_member";
                dataType: "string";
                columnType: "SQLiteText";
                data: string;
                driverParam: string;
                notNull: false;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: [string, ...string[]];
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {
                length: number | undefined;
            }>;
            invitedAt: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "invited_at";
                tableName: "workspace_member";
                dataType: "date";
                columnType: "SQLiteTimestamp";
                data: Date;
                driverParam: number;
                notNull: true;
                hasDefault: true;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: undefined;
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {}>;
            acceptedAt: drizzle_orm_sqlite_core.SQLiteColumn<{
                name: "accepted_at";
                tableName: "workspace_member";
                dataType: "date";
                columnType: "SQLiteTimestamp";
                data: Date;
                driverParam: number;
                notNull: false;
                hasDefault: false;
                isPrimaryKey: false;
                isAutoincrement: false;
                hasRuntimeDefault: false;
                enumValues: undefined;
                baseColumn: never;
                identity: undefined;
                generated: undefined;
            }, {}, {}>;
        };
        dialect: "sqlite";
    }>;
};
type TeamTables = ReturnType<typeof createTeamTables>;
type OrganizationRow = TeamTables['organizations']['$inferSelect'];
type OrganizationMemberRow = TeamTables['organizationMembers']['$inferSelect'];
type WorkspaceMemberRow = TeamTables['workspaceMembers']['$inferSelect'];

/**
 * RBAC access builders over the teams tables. `createWorkspaceAccess` and
 * `createOrganizationAccess` close over the product's `db`, the tables from
 * `createTeamTables`, and the product's `workspace` table, returning the exact
 * `getWorkspaceAccess` / `requireWorkspaceAccess` / `listUserWorkspaces` /
 * `getOrganizationAccess` / `requireOrganizationAccess` functions a consumer
 * already calls — so adoption is a one-line import swap, every call site stays
 * identical.
 *
 * Defense in depth: org owners/admins are workspace owners across the org;
 * everyone else gets their explicit per-workspace role. Effective role is the
 * fold in `resolveWorkspaceRole` (pure, from `../roles`). Every query pins
 * `userId`, so a leaked id can never read across the tenancy boundary — it
 * surfaces as "not found".
 *
 * Driver-agnostic: builders are awaited, never `.run()`/`.all()`, so
 * better-sqlite3, D1, and libsql handles all behave identically.
 */

/** Any SQLite drizzle database — `any` erases driver-specific generics so
 *  better-sqlite3, D1, and libsql handles all fit. */
type TeamDatabase = BaseSQLiteDatabase<'sync' | 'async', any, any>;
/**
 * The product's workspace table, narrowed to the columns the access joins read.
 * Adopters pass their real drizzle workspace table — it carries these columns
 * (gtm's does); the type only asserts the minimum the joins touch.
 */
interface WorkspaceAccessTable {
    id: any;
    organizationId: any;
    name: any;
    updatedAt: any;
}
interface CreateAccessOptions {
    db: TeamDatabase;
    tables: TeamTables;
    /** The product's workspace table (the FK target passed to createTeamTables). */
    workspaceTable: TeamParentTable & WorkspaceAccessTable;
}
interface WorkspaceAccess {
    workspace: Record<string, unknown>;
    organization: OrganizationRow;
    organizationMember: OrganizationMemberRow;
    role: WorkspaceRole;
}
interface UserWorkspaceSummary extends Record<string, unknown> {
    organizationId: string;
    organizationName: string;
    role: WorkspaceRole;
}
interface WorkspaceAccessApi {
    getWorkspaceAccess(workspaceId: string, userId: string, minRole?: WorkspaceRole): Promise<WorkspaceAccess | null>;
    requireWorkspaceAccess(workspaceId: string, userId: string, minRole?: WorkspaceRole): Promise<WorkspaceAccess>;
    listUserWorkspaces(userId: string): Promise<UserWorkspaceSummary[]>;
}
declare function createWorkspaceAccess(opts: CreateAccessOptions): WorkspaceAccessApi;
interface OrganizationAccess {
    organization: OrganizationRow;
    member: OrganizationMemberRow;
    role: OrganizationRole;
}
interface OrganizationAccessApi {
    getOrganizationAccess(organizationId: string, userId: string, minRole?: OrganizationRole): Promise<OrganizationAccess | null>;
    requireOrganizationAccess(organizationId: string, userId: string, minRole?: OrganizationRole): Promise<OrganizationAccess>;
}
interface CreateOrganizationAccessOptions {
    db: TeamDatabase;
    tables: TeamTables;
}
declare function createOrganizationAccess(opts: CreateOrganizationAccessOptions): OrganizationAccessApi;

export { type CreateAccessOptions as C, type OrganizationRow as O, type TeamDatabase as T, type UserWorkspaceSummary as U, type WorkspaceAccess as W, type TeamTables as a, type OrganizationMemberRow as b, type CreateOrganizationAccessOptions as c, type CreateTeamTablesOptions as d, type OrganizationAccess as e, type OrganizationAccessApi as f, type TeamParentTable as g, type WorkspaceAccessApi as h, type WorkspaceAccessTable as i, type WorkspaceMemberRow as j, createOrganizationAccess as k, createTeamTables as l, createWorkspaceAccess as m };
