import _ from 'lodash';
import {errors, validateExecuteMethodParams} from '../../protocol';
import type {
  Constraints,
  Driver,
  DriverClass,
  DriverCommand,
  IExecuteCommands,
  StringRecord,
} from '@appium/types';
import {rankLevenshteinCandidates} from '../../helpers/levenshtein-match';
import {mixin} from './mixin';
import type {BaseDriver} from '../driver';

declare module '../driver' {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  interface BaseDriver<C extends Constraints> extends IExecuteCommands {}
}

const ExecuteCommands: IExecuteCommands = {
  async executeMethod<C extends Constraints>(
    this: BaseDriver<C>,
    script: string,
    protoArgs: readonly [StringRecord<unknown>] | readonly unknown[]
  ) {
    const Driver = this.constructor as DriverClass<Driver<C>>;
    const commandMetadata = {...Driver.executeMethodMap?.[script]};
    if (!commandMetadata.command) {
      const availableScripts = _.keys(Driver.executeMethodMap);
      if (_.isEmpty(availableScripts)) {
        throw new errors.UnsupportedOperationError(
          `Unsupported execute method '${script}'. ` +
          `Make sure the installed ${Driver.name} is up-to-date. ` +
          `The current driver version does not define any execute methods.`
        );
      }
      const {sorted: sortedMatches, suggestion} = rankLevenshteinCandidates(script, availableScripts);
      throw new errors.UnsupportedOperationError(
        (suggestion
          ? `Unsupported execute method '${script}', did you mean '${suggestion}'? `
          : `Unsupported execute method '${script}'. `) +
        `Make sure the installed ${Driver.name} is up-to-date. ` +
        `Execute methods available in the current driver version are: ` +
        sortedMatches.join(', ')
      );
    }
    const args = validateExecuteMethodParams(protoArgs as any[], commandMetadata.params);
    const commandName = commandMetadata.command as keyof BaseDriver<C>;
    const command = this[commandName] as DriverCommand;
    return await command.call(this, ...args);
  },
};

mixin(ExecuteCommands);
