import { UniDriver } from '@wix/wix-ui-test-utils/unidriver';
import { CheckboxUniDriver } from '@wix/design-system/dist/testkit/unidriver';
import { baseUniDriverFactory } from '../../../../unidriver';

export function BooleanEditUniDriver(base: UniDriver, body: UniDriver) {
  const checkbox = CheckboxUniDriver(
    base.$('[data-hook="boolean-checkbox"]'),
    body,
  );

  return {
    ...baseUniDriverFactory(base),
    isChecked: () => checkbox.isChecked(),
    toggle: () => checkbox.click(),
    /** Native dispatch — Checkbox uni.driver has no keyDown */
    keyDown: async (
      key: string,
      opts?: Pick<
        KeyboardEventInit,
        'shiftKey' | 'ctrlKey' | 'metaKey' | 'altKey'
      >,
    ) => {
      const input = await base.$('input').getNative();
      input.dispatchEvent(
        new KeyboardEvent('keydown', { key, bubbles: true, ...opts }),
      );
    },
  };
}
