import 'events';
import { TypedEmitter } from 'tiny-typed-emitter';
import GC from '@grapecity/spread-sheets';
export interface SheetRange {
    row: number;
    col: number;
    rowCount: number;
    colCount: number;
}
export interface OnesTableEvents {
    'load': (table: OnesTable) => void;
    'reload': (table: OnesTable) => void;
    'error': (table: OnesTable, error: Error) => void;
    'close': (table: OnesTable, event: unknown) => void;
    'undoStackChanged': (table: OnesTable) => void;
    'selectionChanged': (table: OnesTable, sheetName: string, selections: SheetRange[]) => void;
    'userChanged': (table: OnesTable, users: UserData[]) => void;
    'customMessage': (table: OnesTable, message: CustomMessage) => void;
    'statusChanged': (table: OnesTable, status: 'dirty' | 'clean') => void;
    'showContextMenu': (table: OnesTable, menuData: GC.Spread.Sheets.ContextMenu.IMenuItemData[], itemsDataForShown: GC.Spread.Sheets.ContextMenu.IMenuItemData[], hitInfo: object, spread: object) => boolean;
    'localCommand': (table: OnesTable, command: TableCommandData) => void;
    'remoteCommand': (table: OnesTable, command: CommandMessageData) => void;
    'activeSheetChanged': (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet) => void;
    'cellHover': (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, row: number, col: number, hitInfo: OnesTableCellHitInfo) => void;
    'cellImageClick': (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, row: number, col: number) => void;
    'floatingImageClick': (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, picture: GC.Spread.Sheets.FloatingObjects.Picture) => void;
    'metaChanged': (table: OnesTable, name: string, value: string) => void;
}
export interface OnesTableCustom {
    destroy: () => void;
}
export interface DocVersion {
    kbId: string;
    docId: string;
    version: number;
    userId: string;
    meta: string;
    content?: string;
    title?: string;
    text?: string;
}
export interface TableAction {
    kbId: string;
    docId: string;
    version: number;
    userId: string;
    action: WorkbookActionData;
    createdAt: number;
}
export interface CellAction {
    userId: string;
    kbId: string;
    docId: string;
    version: number;
    cellId: string;
    value: unknown;
    createdAt: number;
}
export interface FileInfo {
    hash: string;
    size: number;
    createdAt: number;
}
export interface ResourceInfo {
    kbId: string;
    docId: string;
    hash: string;
    size: number;
    mimetype: string;
    filename: string;
    createdAt: number;
    userId: string;
}
export type OnProgress = (percent: number) => void;
export interface OnesTableSettings {
    get: (key: string, defaultValue?: unknown) => unknown;
    set: (key: string, value: unknown) => void;
    remove: (key: string) => void;
}
export interface TableSidebarOptions {
    id: string;
    onShow?: (sidebar: HTMLElement) => void;
}
export type OnesTableCellHitInfo = GC.Spread.Sheets.IHitTestCellTypeHitInfo;
export interface OnesTableHook {
    cellMouseEnter?: (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, hitInfo: OnesTableCellHitInfo) => boolean | undefined;
    cellMouseLeave?: (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, hitInfo: OnesTableCellHitInfo) => boolean | undefined;
    cellMouseDown?: (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, hitInfo: OnesTableCellHitInfo) => boolean | undefined;
    cellMouseUp?: (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, hitInfo: OnesTableCellHitInfo) => boolean | undefined;
    cellMouseMove?: (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, hitInfo: OnesTableCellHitInfo) => boolean | undefined;
    cellPaint?: (table: OnesTable, oldPaint: any, ctx: CanvasRenderingContext2D, value: any, x: number, y: number, w: number, h: number, style: GC.Spread.Sheets.Style, context?: any) => boolean | undefined;
    getCellHitInfo?: (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, hitInfo: OnesTableCellHitInfo, x: number, y: number) => GC.Spread.Sheets.IHitTestCellTypeHitInfo | undefined;
}
export type ToastType = 'error' | 'success' | 'warning' | 'info';
export interface ToastInfo {
    type: ToastType;
    message: string;
}
export interface OnesTable extends TypedEmitter<OnesTableEvents> {
    destroy: () => void;
    workbook: () => GC.Spread.Sheets.Workbook;
    server: () => string;
    token: () => string;
    clientId: () => string;
    docId: () => string;
    user: () => UserData;
    cellMasks: () => CellMasks;
    setReadonly: (readonly: boolean) => void;
    readonly: () => boolean;
    broadcastCustomMessage: (data: CustomMessageData) => void;
    handleSelectionChanged: (sheetName: string, newSelections: SheetRange[]) => void;
    getUserColor: (userId: string) => string;
    queryCellActions: (sheetName: string, row: number, col: number) => Promise<any[]>;
    createVersion: (options?: {
        name?: string;
        tag?: string;
        type?: string;
    }) => Promise<DocVersion>;
    queryVersions: () => Promise<DocVersion[]>;
    queryVersion: (version: number, options?: {
        withData: boolean;
    }) => Promise<DocVersion | null>;
    restoreVersion: (version: number) => Promise<DocVersion>;
    addCustom: <T extends OnesTableCustom>(name: string, creator: () => T) => T;
    getCustom: <T extends OnesTableCustom>(name: string) => T | undefined;
    getMeta: (name: string) => string | undefined;
    setMeta: (name: string, value: string) => void;
    uploadResource: (file: File, onProgress?: OnProgress) => Promise<string>;
    getResourceUrl: (resourceId: string, options?: {
        withToken?: boolean;
    }) => string;
    getResourceId: (url: string) => string;
    getSettings: () => OnesTableSettings;
    setCurrentCommand: (command: string) => void;
    getCurrentCommand: () => string;
    showSidebar: (content: HTMLElement, options: TableSidebarOptions) => void;
    closeSidebar: () => void;
    addHook: (hook: OnesTableHook) => void;
    getHooks: () => OnesTableHook[];
    showToast: (options: ToastInfo) => void;
    toExcel: () => Promise<Blob>;
    i18nt: (key: string, args?: Record<string, string>) => string;
    get options(): LoadTableOptions;
}
export interface WorkbookActionData {
    o: string;
}
export type Operation = 'create' | 'import' | 'copy' | 'restore' | 'meta';
export interface WorkbookActionDataEx extends WorkbookActionData {
    o: Operation;
    data?: unknown;
}
export interface WorkbookEvent {
    name: string;
    reexecutable: boolean;
    toJson: (table: OnesTable, args: any, workbook: GC.Spread.Sheets.Workbook) => WorkbookActionData | undefined | Promise<WorkbookActionData | undefined>;
    executeJson: (op: WorkbookActionData, workbook: GC.Spread.Sheets.Workbook) => void;
}
export interface WorkbookCommandArgs {
    cmd: string;
    sheetName: string;
    isUndo?: boolean;
}
export interface WorkbookCommand {
    name: string;
    reexecutable: boolean;
    canUndo?: boolean;
    toJson?: (table: OnesTable, command: any, workbook: GC.Spread.Sheets.Workbook) => WorkbookActionData | undefined | Promise<WorkbookActionData | undefined>;
    executeJson?: (op: WorkbookActionData, workbook: GC.Spread.Sheets.Workbook) => void;
    execute: (context: GC.Spread.Sheets.Workbook, options: any, isUndo: boolean) => boolean;
    executeCommand: (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, ...extArgs: any[]) => void;
}
export interface UserData {
    name: string;
    id: string;
    avatar: string;
}
export interface DocInfo {
    kbId: string;
    id: string;
}
export interface ConnectedMessageData {
    table: object;
    user: UserData;
    clients: ClientInfo[];
    resourceToken: string;
}
export interface CommandMessageData {
    data: WorkbookActionData;
}
export type CustomMessageType = 'remoteSelectionChanged' | 'userLeave' | 'userJoin';
export interface CustomMessageData {
    type: CustomMessageType;
    data: unknown;
}
export interface CustomSelectionChangedData {
    clientId: string;
    user: UserData;
    sheetName: string;
    selections: SheetRange[];
}
export interface CustomUserJoinData {
    user: UserData;
}
export interface CustomUserLeaveData {
    user: UserData;
}
export interface SheetData {
    name: string;
    rows: number;
    cols: number;
    text: string[][];
}
export interface TableDataAbstract {
    sheets: SheetData[];
}
export interface Message {
    type: 'connect' | 'connected' | 'command' | 'error' | 'custom' | 'received' | 'meta';
    clientId: string;
    docId: string;
    version: number;
    data: unknown;
    verifyData?: TableDataAbstract;
}
export interface ConnectMessage extends Message {
    type: 'connect';
    token: string;
    kbId: string;
}
export interface ConnectedMessage extends Message {
    type: 'connected';
    data: ConnectedMessageData;
    kbId: string;
}
export interface CommandMessage extends Message {
    type: 'command';
    data: CommandMessageData;
    status?: 'sending' | 'received';
}
export interface ReceivedMessage extends Message {
    type: 'received';
    data: null;
    version: number;
}
export interface CustomMessage extends Message {
    type: 'custom';
    version: 0;
    data: CustomMessageData;
}
export interface MetaMessageData {
    name: 'DocTitle' | string;
    value: string;
}
export interface MetaMessage extends Message {
    type: 'meta';
    version: number;
    data: MetaMessageData;
}
export interface ClientInfo {
    clientId: string;
    user: UserData;
    selections: CustomSelectionChangedData;
}
export declare enum ErrorCode {
    UnknownMessage = "UnknownMessage",
    ConnectError = "ConnectError",
    WebSocketError = "WebSocketError",
    AuthError = "AuthError",
    NotFoundError = "NotFoundError",
    InvalidParamError = "InvalidParamError",
    ServerError = "ServerError",
    ConflictError = "ConflictError",
    StorageError = "StorageError"
}
export interface ErrorMessage extends Message {
    type: 'error';
    code: ErrorCode;
    message: string;
    tableData?: object;
    reason?: string;
}
export interface TableCommand {
    cmd?: string;
}
export declare enum TableCommandActionType {
    Normal = 0,
    Undo = 1,
    Redo = 2
}
export interface TableCommandData {
    actionType: number;
    command: TableCommand;
}
export interface CommandManager {
    addListener: (name: string, callback: (data: TableCommandData) => void) => void;
    removeListener: (name: string) => void;
    execute(commandOptions: unknown): boolean;
}
export interface SheetEventArgs {
    propertyName: string;
}
export interface OnesTableCallbacks {
    onPaste?: (table: OnesTable, e: ClipboardEvent) => boolean;
}
export type OnesTableAttachmentUploadProgress = (index: number, percent: number) => void;
export interface OnesTableAttachmentItem {
    id: string;
    name: string;
    size: number;
    type: string;
}
export interface OnesTableAttachmentHelperOptions {
    previewEnabled?: boolean;
}
export interface OnesTableAttachmentHelper {
    options: (table: OnesTable) => OnesTableAttachmentHelperOptions;
    uploadAttachments: (table: OnesTable, files: File[], onProgress: OnesTableAttachmentUploadProgress) => Promise<OnesTableAttachmentItem[]>;
    previewAttachment: (table: OnesTable, attachment: OnesTableAttachmentItem) => void;
    downloadAttachment: (table: OnesTable, attachment: OnesTableAttachmentItem) => void;
}
export interface InsertPictureOptions {
    row: number;
    col: number;
    type: 'cell' | 'float';
    fileOrUrl: string | File;
    fileName?: string;
}
export interface LoadTableOptions {
    server: string;
    debug?: boolean;
    token: string;
    colors: string[];
    settings?: OnesTableSettings;
    reauth?: () => Promise<string>;
    showSidebar: (content: HTMLElement, options: TableSidebarOptions) => void;
    showToast: (options: ToastInfo) => void;
    closeSidebar: () => void;
    insertPicture: (table: OnesTable, sheet: GC.Spread.Sheets.Worksheet, options: InsertPictureOptions) => Promise<boolean>;
    callbacks?: OnesTableCallbacks;
    attachmentHelper?: OnesTableAttachmentHelper;
    limit?: {
        image?: number;
    };
}
export interface CellMaskOptions {
    type: string;
    id: string;
    borderColor: string;
    borderSize: number;
    classNames?: string[];
    data?: unknown;
    onCreate?: (mark: CellMask, parent: HTMLElement) => void;
}
export interface CellMask {
    options: CellMaskOptions;
    sheetName: string;
    col: number;
    row: number;
    colCount: number;
    rowCount: number;
    getElement: () => HTMLElement;
    moveToCell: (sheetName: string, row: number, col: number, rowCount: number, colCount: number) => void;
    getClientRect: () => DOMRect;
    updateLayout: () => void;
    setHover: (hover: boolean) => void;
    setActive: (active: boolean) => void;
    setEditing: (editing: boolean) => void;
    setVisible: (visible: boolean) => void;
    isHover: () => boolean;
    isActive: () => boolean;
    isEditing: () => boolean;
    isVisible: () => boolean;
    destroy: () => void;
}
export interface CellMasks {
    destroy: () => void;
    add: (maskOptions: CellMaskOptions) => CellMask;
    find: (id: string) => CellMask | undefined;
    remove: (id: string) => void;
    all: () => CellMask[];
}
export interface MenuItemCommandOptions {
    icon?: string;
    shortcut?: string;
}
export interface MenuItemData {
    name: string;
    text: string;
    command: string;
    workArea: string;
    canUndo?: boolean;
    iconClass?: string;
    type?: string;
    commandOptions?: MenuItemCommandOptions;
    subMenu?: MenuItemData[];
    execute?: (context: GC.Spread.Sheets.Workbook, options: unknown, isUndo: boolean) => boolean;
    createMenuItemElement?: (context: OnesTable, options: GC.Spread.Sheets.ContextMenu.IMenuItemData) => HTMLElement | DocumentFragment;
}
export interface IMenuItemDataEx extends GC.Spread.Sheets.ContextMenu.IMenuItemData {
    createMenuItemElement?: (context: OnesTable, options: GC.Spread.Sheets.ContextMenu.IMenuItemData) => HTMLElement | DocumentFragment;
}
export interface SortItemData {
    column: number;
    type: 'asc' | 'desc';
}
