import _ from 'lodash';
import {assertSimulator as _assertSimulator} from '../utils';
import { errors } from 'appium/driver';
import type {XCUITestDriver} from '../driver';
import type {IncreaseContrastAction, IncreaseContrastResult} from './types';
import type {Simulator} from 'appium-ios-simulator';

const assertSimulator = (driver: XCUITestDriver): Simulator => _assertSimulator.call(driver, 'Content size ui command');

const INCREASE_CONTRAST_CONFIG = [
    'enabled',
    'disabled',
] as const;

/**
 * Sets the increase contrast configuration for the given simulator.
 *
 * @since Xcode 15 (but lower xcode could have this command)
 * @param increaseContrast - Valid increase contrast configuration value.
 *                          Acceptable value is 'enabled' or 'disabled' with Xcode 16.2.
 * @throws If the current platform does not support content size appearance changes
 */
export async function mobileSetIncreaseContrast(
  this: XCUITestDriver,
  increaseContrast: IncreaseContrastAction,
): Promise<void> {
  if (!(INCREASE_CONTRAST_CONFIG as readonly string[]).includes(_.lowerCase(increaseContrast))) {
    throw new errors.InvalidArgumentError(
      `The 'increaseContrast' value is expected to be one of ${INCREASE_CONTRAST_CONFIG.join(',')}`
    );
  }

  await assertSimulator(this).setIncreaseContrast(increaseContrast);
}

/**
 * Retrieves the current increase contrast configuration value from the given simulator.
 *
 * @since Xcode 15 (but lower xcode could have this command)
 * @returns The contrast configuration value.
 *          Possible return value is 'enabled', 'disabled',
 *          'unsupported' or 'unknown' with Xcode 16.2.
 */
export async function mobileGetIncreaseContrast(
  this: XCUITestDriver,
): Promise<IncreaseContrastResult> {
  return await assertSimulator(this).getIncreaseContrast() as IncreaseContrastResult;
}

