import { UniDriver } from '@wix/wix-ui-test-utils/unidriver';
import { DropdownLayoutUniDriver } from '@wix/design-system/dist/testkit/unidriver';
import { DropdownLayoutUniDriver as DropdownLayoutUniDriverFactory } from '@wix/design-system/dist/types/DropdownLayout/DropdownLayout.uni.driver';
import { UniDriverFactory } from '../uniDriverFactory';

export function EditableTableContextMenuUniDriver(
  _base: UniDriver,
  body: UniDriver,
) {
  const menuBase = body.$('[data-hook="editable-table-context-menu"]');

  const getDropdownLayout = () =>
    (
      DropdownLayoutUniDriver as UniDriverFactory<DropdownLayoutUniDriverFactory>
    )(menuBase, body);

  return {
    isShown: async () => menuBase.exists(),

    isOptionVisible: async (optionId: string) => {
      if (!(await menuBase.exists())) {
        return false;
      }
      return body.$(`[data-hook="dropdown-item-${optionId}"]`).exists();
    },

    clickOption: async (optionId: string) => {
      const option = await getDropdownLayout().optionById(optionId);
      await option.click();
    },

    getOptionText: async (optionId: string) => {
      const option = await getDropdownLayout().optionById(optionId);
      return option.content();
    },
  };
}
