import { AbstractInterface } from '@midscene/core/device';
import type { ActionParam } from '@midscene/core';
import type { ActionReturn } from '@midscene/core';
import { Agent } from '@midscene/core/agent';
import { AgentOpt } from '@midscene/core/agent';
import { DeviceAction } from '@midscene/core';
import type { ElementInfo } from '@midscene/shared/extractor';
import { HarmonyDeviceOpt } from '@midscene/core/device';
import { InterfaceType } from '@midscene/core';
import { LocateResultElement } from '@midscene/core';
import { overrideAIConfig } from '@midscene/shared/env';
import { Point } from '@midscene/core';
import { Size } from '@midscene/core';

declare type ActionArgs<T extends DeviceAction> = [ActionParam<T>] extends [undefined] ? [] : [ActionParam<T>];

export declare function agentFromHdcDevice(deviceId?: string, opts?: HarmonyAgentOpt & HarmonyDeviceOpt): Promise<HarmonyAgent>;

declare type DeviceActionHarmonyBackButton = DeviceAction<undefined, void>;

declare type DeviceActionHarmonyHomeButton = DeviceAction<undefined, void>;

declare type DeviceActionHarmonyRecentAppsButton = DeviceAction<undefined, void>;

export declare function getConnectedDevices(hdcPath?: string): Promise<HarmonyDeviceInfo[]>;

export declare class HarmonyAgent extends Agent<HarmonyDevice> {
    back: WrappedAction<DeviceActionHarmonyBackButton>;
    home: WrappedAction<DeviceActionHarmonyHomeButton>;
    recentApps: WrappedAction<DeviceActionHarmonyRecentAppsButton>;
    private appNameMapping;
    constructor(device: HarmonyDevice, opts?: HarmonyAgentOpt);
    launch(uri: string): Promise<void>;
    runHdcShell(command: string): Promise<string>;
    private createActionWrapper;
}

export declare type HarmonyAgentOpt = AgentOpt & {
    /**
     * Custom mapping of app names to bundle names
     * User-provided mappings will take precedence over default mappings
     */
    appNameMapping?: Record<string, string>;
};

export declare class HarmonyDevice implements AbstractInterface {
    private deviceId;
    private hdc;
    private connecting;
    private destroyed;
    private descriptionText;
    private customActions?;
    private cachedScreenSize;
    private appNameMapping;
    private lastTapPosition;
    interfaceType: InterfaceType;
    uri: string | undefined;
    options?: HarmonyDeviceOpt;
    actionSpace(): DeviceAction<any>[];
    constructor(deviceId: string, options?: HarmonyDeviceOpt);
    describe(): string;
    connect(): Promise<HdcClient>;
    getHdc(): Promise<HdcClient>;
    setAppNameMapping(mapping: Record<string, string>): void;
    private resolvePackageName;
    launch(uri: string): Promise<HarmonyDevice>;
    getScreenSize(): Promise<{
        width: number;
        height: number;
    }>;
    size(): Promise<Size>;
    private remoteScreenshotPath;
    private localScreenshotPath;
    screenshotBase64(): Promise<string>;
    tap(x: number, y: number): Promise<void>;
    doubleTap(x: number, y: number): Promise<void>;
    longPress(x: number, y: number): Promise<void>;
    inputText(text: string, element?: LocateResultElement, shouldReplace?: boolean): Promise<void>;
    clearInput(element?: ElementInfo): Promise<void>;
    keyboardPress(key: string): Promise<void>;
    scroll(deltaX: number, deltaY: number, speed?: number): Promise<void>;
    private scrollInDirection;
    scrollDown(distance?: number, startPoint?: Point): Promise<void>;
    scrollUp(distance?: number, startPoint?: Point): Promise<void>;
    scrollLeft(distance?: number, startPoint?: Point): Promise<void>;
    scrollRight(distance?: number, startPoint?: Point): Promise<void>;
    private scrollUntilEdge;
    scrollUntilTop(startPoint?: Point): Promise<void>;
    scrollUntilBottom(startPoint?: Point): Promise<void>;
    scrollUntilLeft(startPoint?: Point): Promise<void>;
    scrollUntilRight(startPoint?: Point): Promise<void>;
    back(): Promise<void>;
    home(): Promise<void>;
    recentApps(): Promise<void>;
    hideKeyboard(): Promise<void>;
    getTimestamp(): Promise<number>;
    destroy(): Promise<void>;
}

declare interface HarmonyDeviceInfo {
    deviceId: string;
}

declare class HdcClient {
    private hdcPath;
    private deviceId;
    private timeout;
    private execMutex;
    constructor(options: HdcOptions);
    private buildArgs;
    exec(...args: string[]): Promise<string>;
    shell(command: string): Promise<string>;
    fileSend(localPath: string, remotePath: string): Promise<void>;
    fileRecv(remotePath: string, localPath: string): Promise<void>;
    screenshot(remotePath: string): Promise<string>;
    click(x: number, y: number): Promise<void>;
    doubleClick(x: number, y: number): Promise<void>;
    longClick(x: number, y: number): Promise<void>;
    swipe(fromX: number, fromY: number, toX: number, toY: number, speed?: number): Promise<void>;
    fling(fromX: number, fromY: number, toX: number, toY: number, speed?: number): Promise<void>;
    drag(fromX: number, fromY: number, toX: number, toY: number, speed?: number): Promise<void>;
    inputText(x: number, y: number, text: string): Promise<void>;
    keyEvent(...keys: string[]): Promise<void>;
    /**
     * Clear text field by batch-sending Backspace(2055) and Delete(2071) key
     * events. Deletes both before and after the cursor to ensure all content is
     * removed regardless of cursor position, matching Android's clearTextField.
     */
    clearTextField(length?: number): Promise<void>;
    startAbility(bundleName: string, abilityName: string): Promise<void>;
    queryMainAbility(bundleName: string): Promise<string | undefined>;
    forceStop(bundleName: string): Promise<void>;
    getScreenInfo(): Promise<{
        width: number;
        height: number;
    }>;
    listTargets(): Promise<string[]>;
}

declare interface HdcOptions {
    hdcPath?: string;
    deviceId?: string;
    timeout?: number;
}

export { overrideAIConfig }

declare type WrappedAction<T extends DeviceAction> = (...args: ActionArgs<T>) => Promise<ActionReturn<T>>;

export { }
