import { Logger } from '../logger.js';
import { clusterNames } from './index.js';
const log = Logger.withPrefix('Matter/Switch');
/**
 * Implementation of {@link SwitchAPI}.
 *
 * Translates `press`/`release` into the correct `switch.currentPosition` value
 * (`options.position ?? 1` for press, `0` for release) and delegates to
 * `MatterAPI.updateAccessoryState`. The Matter.js `SwitchServer` reacts to the
 * attribute change and emits the corresponding Switch cluster events
 * (`initialPress`, `shortRelease`, `longRelease`, `multiPressComplete`).
 */
export class SwitchAPIImpl {
    matterApi;
    constructor(matterApi) {
        this.matterApi = matterApi;
    }
    async emit(uuid, action, options) {
        if (!uuid) {
            log.error('switch.emit: uuid parameter is required');
            return;
        }
        if (action !== 'press' && action !== 'release') {
            log.error(`switch.emit: invalid action "${action}" — must be "press" or "release"`);
            return;
        }
        let position;
        if (action === 'press') {
            const rawPosition = options?.position ?? 1;
            if (!Number.isInteger(rawPosition) || rawPosition < 1) {
                log.warn(`switch.emit: invalid position ${rawPosition} — must be a finite integer >= 1; defaulting to 1`);
                position = 1;
            }
            else {
                position = rawPosition;
            }
        }
        else {
            position = 0;
        }
        const partId = options?.partId;
        log.debug(`Emitting switch ${action} for accessory ${uuid}: currentPosition=${position}${partId ? `, partId=${partId}` : ''}`);
        await this.matterApi.updateAccessoryState(uuid, clusterNames.Switch, { currentPosition: position }, partId);
    }
    async emitGesture(uuid, gesture, options) {
        if (!uuid) {
            log.error('switch.emitGesture: uuid parameter is required');
            return;
        }
        if (gesture !== 'singlePress' && gesture !== 'doublePress' && gesture !== 'longPress') {
            log.error(`switch.emitGesture: invalid gesture "${gesture}" — must be "singlePress", "doublePress", or "longPress"`);
            return;
        }
        const emitOpts = { position: options?.position, partId: options?.partId };
        switch (gesture) {
            case 'singlePress': {
                await this.emit(uuid, 'press', emitOpts);
                await this.emit(uuid, 'release', emitOpts);
                break;
            }
            case 'doublePress': {
                const multiPressDelayMs = options?.multiPressDelayMs ?? 100;
                await this.emit(uuid, 'press', emitOpts);
                await this.emit(uuid, 'release', emitOpts);
                await new Promise(resolve => setTimeout(resolve, multiPressDelayMs));
                await this.emit(uuid, 'press', emitOpts);
                await this.emit(uuid, 'release', emitOpts);
                break;
            }
            case 'longPress': {
                const longPressDelayMs = options?.longPressDelayMs ?? 2500;
                await this.emit(uuid, 'press', emitOpts);
                await new Promise(resolve => setTimeout(resolve, longPressDelayMs));
                await this.emit(uuid, 'release', emitOpts);
                break;
            }
        }
    }
}
//# sourceMappingURL=SwitchAPI.js.map