import { RicosPluginToolbarNames } from "./../ricos-types/index.js";
import { PLUGIN_TOOLBAR_DELETE_BUTTON } from "./../_shared/data-hooks.js";
import { dataHook } from "./../wix-rich-content-common/index.js";
import { DATA_HOOKS } from "./data-hooks.js";
const createSmartBlockTestkit = (getBase) => {
  const getSmartBlock = async (index) => {
    const smartBlocks = await getBase().getAll(dataHook(DATA_HOOKS.SMART_BLOCK));
    return smartBlocks.at(index);
  };
  const getVariantSwitcherPanel = () => {
    return getBase().$(dataHook(DATA_HOOKS.INSERT_MODAL));
  };
  const getVariantOption = (variant) => {
    return getVariantSwitcherPanel().$(dataHook(`variant-option-${variant}`));
  };
  return {
    content: {
      atIndex: (index) => ({
        exists: async () => {
          try {
            const smartBlock = await getSmartBlock(index);
            return smartBlock.exists();
          } catch {
            return false;
          }
        },
        click: async () => {
          const smartBlock = await getSmartBlock(index);
          await smartBlock.click();
        },
        getVariantKey: async () => {
          const smartBlock = await getSmartBlock(index);
          return smartBlock.attr("data-type");
        },
        getCellCount: async () => {
          const smartBlock = await getSmartBlock(index);
          const cells = smartBlock.$$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL));
          return cells.count();
        },
        getCellAtIndex: (cellIndex) => {
          const getCell = async () => {
            const smartBlock = await getSmartBlock(index);
            const cells = smartBlock.$$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL));
            return cells.at(cellIndex);
          };
          return {
            exists: async () => {
              try {
                const cell = await getCell();
                return cell.exists();
              } catch {
                return false;
              }
            },
            getDragHandle: () => ({
              exists: async () => {
                try {
                  const cell = await getCell();
                  return cell.$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL_HANDLE)).exists();
                } catch {
                  return false;
                }
              },
              click: async () => {
                const cell = await getCell();
                await cell.$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL_HANDLE)).click();
              }
            }),
            getAddButton: () => ({
              exists: async () => {
                try {
                  const smartBlock = await getSmartBlock(index);
                  const cells = smartBlock.$$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL));
                  const totalCells = await cells.count();
                  if (cellIndex !== totalCells - 1) {
                    return false;
                  }
                  return smartBlock.$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL_ADD_BUTTON)).exists();
                } catch {
                  return false;
                }
              },
              click: async () => {
                const smartBlock = await getSmartBlock(index);
                await smartBlock.$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL_ADD_BUTTON)).$("button").click();
              }
            }),
            getDeleteButton: () => ({
              exists: async () => {
                try {
                  const cell = await getCell();
                  return cell.$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL_HANDLE)).exists();
                } catch {
                  return false;
                }
              },
              click: async () => {
                const cell = await getCell();
                await cell.$(dataHook(DATA_HOOKS.SMART_BLOCK_CELL_HANDLE)).click();
                await getBase().$(dataHook(PLUGIN_TOOLBAR_DELETE_BUTTON)).click();
              }
            })
          };
        }
      })
    },
    toolbars: {
      createPluginToolbar: (getPluginToolbarBase) => ({
        openVariantSwitcher: async () => {
          const button = await getPluginToolbarBase(RicosPluginToolbarNames.SMART_BLOCK).get(
            dataHook(DATA_HOOKS.VARIANT_BUTTON)
          );
          await button.click();
        },
        exists: async () => {
          try {
            return getPluginToolbarBase(RicosPluginToolbarNames.SMART_BLOCK).exists();
          } catch {
            return false;
          }
        }
      })
    },
    variantSwitcherPanel: {
      exists: async () => {
        try {
          return getVariantSwitcherPanel().exists();
        } catch {
          return false;
        }
      },
      waitForElement: async () => {
        const panel = getVariantSwitcherPanel();
        await panel.wait();
      },
      selectVariant: async (variant) => {
        const option = getVariantOption(variant);
        await option.click();
      },
      getSelectedVariant: async () => {
        return null;
      }
    }
  };
};
export {
  createSmartBlockTestkit
};
//# sourceMappingURL=testkit.js.map
