import { RicosPluginToolbarNames } from "./../ricos-types/index.js";
import { LINK_PLUGIN_WEB_LINK_BUTTON, PLUGIN_TOOLBAR_SETTINGS_BUTTON } from "./../_shared/data-hooks.js";
import {
  createLinkModalUnidriver
} from "./../_shared/link-modal/create-link-modal-unidriver.js";
import { dataHook } from "./../wix-rich-content-common/index.js";
import {
  ANCHOR_LINK,
  LINK_PLUGIN_REMOVE_BUTTON,
  LINK_PLUGIN_SCROLL_TO_ANCHOR_BUTTON,
  TEXT_BUTTON_LINK,
  WEB_LINK
} from "./data-hooks.js";
const createLinkTestkit = (getBase) => {
  const getLinkAtIndex = (type) => (index) => {
    const linkDataHook = type === "anchor" ? ANCHOR_LINK : WEB_LINK;
    const getLink = () => {
      const link = getBase().$$(dataHook(linkDataHook));
      return link.get(index);
    };
    return {
      exists: async () => {
        const link = getLink();
        return link.exists();
      },
      getText: async () => {
        const link = getLink();
        return link.text();
      },
      getHref: async () => {
        const link = getLink();
        return link.attr("href");
      },
      getRel: async () => {
        const link = getLink();
        return link._prop("rel");
      },
      getTarget: async () => {
        const link = getLink();
        return link._prop("target");
      },
      click: async () => {
        const link = getLink();
        await link.click();
      }
    };
  };
  return {
    content: {
      webLink: { atIndex: getLinkAtIndex("web") },
      anchor: { atIndex: getLinkAtIndex("anchor") }
    },
    modal: createLinkModalUnidriver(getBase),
    toolbars: {
      createPluginToolbar: (getPluginToolbarBase) => {
        const getLinkToolbar = () => getPluginToolbarBase(RicosPluginToolbarNames.LINK);
        return {
          exists: async () => {
            try {
              return getLinkToolbar().exists();
            } catch {
              return false;
            }
          },
          waitForElement: async () => {
            const linkToolbar = getLinkToolbar();
            await linkToolbar.wait();
            return linkToolbar;
          },
          getButtonsAmount: async () => {
            const buttons = getLinkToolbar().$$("button");
            return await buttons.count();
          },
          scrollToElementByIndex: async (index) => {
            const button = getLinkToolbar().$$("button").get(index);
            if (!button) {
              throw new Error(`Link toolbar doesn't have a button at index ${index}`);
            }
            await button.scrollIntoView?.();
          },
          clickEdit: async () => {
            const editButton = await getLinkToolbar().get(dataHook(PLUGIN_TOOLBAR_SETTINGS_BUTTON));
            await editButton.click();
          },
          clickRemove: async () => {
            const deleteButton = await getLinkToolbar().get(dataHook(LINK_PLUGIN_REMOVE_BUTTON));
            await deleteButton.click();
          },
          webLink: /* @__PURE__ */ (() => {
            const getElement = () => getLinkToolbar().get(dataHook(LINK_PLUGIN_WEB_LINK_BUTTON));
            return {
              getElement,
              exists: async () => {
                try {
                  const webLink = await getElement();
                  return webLink.exists();
                } catch {
                  return false;
                }
              }
            };
          })(),
          anchorLink: {
            exists: async () => {
              try {
                const anchorLink = await getLinkToolbar().get(
                  dataHook(LINK_PLUGIN_SCROLL_TO_ANCHOR_BUTTON)
                );
                return anchorLink.exists();
              } catch {
                return false;
              }
            },
            clickGoToLink: async () => {
              const anchorLink = await getLinkToolbar().get(
                dataHook(LINK_PLUGIN_SCROLL_TO_ANCHOR_BUTTON)
              );
              await anchorLink.click();
            }
          }
        };
      },
      createTextButtonToolbars: (getTextButtonBase) => ({
        click: async () => {
          const linkButton = getTextButtonBase().$(dataHook(TEXT_BUTTON_LINK));
          await linkButton.wait();
          await linkButton.click();
        }
      })
    }
  };
};
export {
  createLinkTestkit
};
//# sourceMappingURL=testkit.js.map
