/*
This file is part of the Notesnook project (https://notesnook.com/)

Copyright (C) 2023 Streetwriters (Private) Limited

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import { Editor, getAttributes } from "@tiptap/core";
import { MarkType } from "@tiptap/pm/model";
import { Plugin, PluginKey } from "@tiptap/pm/state";

type ClickHandlerOptions = {
  type: MarkType;
  editor: Editor;
};

export function clickHandler(options: ClickHandlerOptions): Plugin {
  return new Plugin({
    key: new PluginKey("handleClickLink"),
    props: {
      handleDOMEvents: {
        click: (view, event) => {
          const isMainClick = event.button === 0;
          const isAuxClick = event.button === 1;
          if (!isMainClick && !isAuxClick) {
            return false;
          }

          let a = event.target as HTMLElement;
          const els = [];

          while (a.nodeName !== "DIV") {
            els.push(a);
            a = a.parentNode as HTMLElement;
          }

          if (!els.find((value) => value.nodeName === "A")) {
            return false;
          }

          const attrs = getAttributes(view.state, options.type.name);
          const link = event.target as HTMLLinkElement;

          const href = link?.href ?? attrs.href;
          // const target = link?.target ?? attrs.target;

          if (link && href) {
            if (options.editor.storage.openLink) {
              event.preventDefault();
              setTimeout(() =>
                options.editor.storage.openLink?.(
                  href,
                  isAuxClick || event.ctrlKey || event.metaKey
                )
              );
            }

            return true;
          }

          return false;
        }
      }
    }
  });
}
