/**
 * @name MoreDoubleClicks
 * @description Allows you to double-click more areas with modifier keys for different actions.
 * @author Kaan
 * @version 3.0.5
 * @source https://github.com/zrodevkaan/BDPlugins/tree/main/Plugins/MoreDoubleClicks/MoreDoubleClicks.plugin.js
 * @invite t3zMgv7Nvb
 * @stable 605958
 * @canary 606445
 */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/MoreDoubleClicks/index.tsx
var index_exports = {};
__export(index_exports, {
  default: () => MoreDoubleClicks
});
module.exports = __toCommonJS(index_exports);
var { Webpack, Utils, Patcher, Data, React, Hooks, Components } = new BdApi("MoreDoubleClicks");
var EditUtils = Webpack.getModule((x) => x.startEditMessageRecord);
var EmojiPack = () => {
  let a = Webpack.getModule((m) => m.EMOJI_NAME_RE && m.getCategories);
  return a.getCategories().map((m) => a.getByCategory(m)).flat();
};
var addReaction = Webpack.getByStrings("uaUU/g", { searchExports: true });
var Permissions = Webpack.getByKeys("BAN_MEMBERS", { searchExports: true });
var { ChannelStore, UserStore, RawGuildEmojiStore, GuildStore, PermissionStore } = Webpack.Stores;
var { useStateFromStores } = Hooks;
var DataStore = new Proxy(
  {},
  {
    get: (_, key) => {
      return Data.load(key);
    },
    set: (_, key, value) => {
      Data.save(key, value);
      return true;
    },
    deleteProperty: (_, key) => {
      Data.delete(key);
    }
  }
);
var MoreDoubleClickStore = new class MDCS extends Utils.Store {
  constructor() {
    super();
    this.deleteKeyPressed = false;
  }
  setSetting(key, value) {
    DataStore.settings = { ...DataStore.settings, [key]: value };
    this.emitChange();
  }
  getSetting(key) {
    return (DataStore.settings ?? {})[key];
  }
  settings() {
    return DataStore.settings;
  }
  setDeleteKeyPressed(pressed) {
    this.deleteKeyPressed = pressed;
  }
  isDeleteKeyPressed() {
    return this.deleteKeyPressed;
  }
}();
function hasPermission(userId, permission, channelId) {
  if (userId === UserStore.getCurrentUser().id) {
    return PermissionStore.can(permission, channelId);
  }
  return PermissionStore.can(permission, channelId, userId);
}
var ReplyAction = Webpack.getByStrings("showMentionToggle", "FOCUS_CHANNEL_TEXT_AREA", { searchExports: true });
function StartDoubleClickAction(_, args, ret, event, message) {
  const canEdit = message.author.id == UserStore.getCurrentUser().id;
  const doubleClickEmoji = MoreDoubleClickStore.getSetting("doubleClickEmoji");
  const textOverride = MoreDoubleClickStore.getSetting("textOverride");
  const selection = window.getSelection();
  if (selection && selection.toString().length > 0 && message.content.includes(selection.toString()) && !textOverride) {
    return;
  }
  const shiftAction = MoreDoubleClickStore.getSetting("shiftDoubleClickAction");
  const ctrlAction = MoreDoubleClickStore.getSetting("ctrlDoubleClickAction");
  const delAction = MoreDoubleClickStore.getSetting("delDoubleClickAction");
  const normalAction = MoreDoubleClickStore.getSetting("normalDoubleClickAction");
  let actionToTake = normalAction;
  if (event.shiftKey) {
    actionToTake = shiftAction;
  } else if (event.ctrlKey || event.metaKey) {
    actionToTake = ctrlAction;
  } else if (MoreDoubleClickStore.isDeleteKeyPressed()) {
    actionToTake = delAction;
  }
  if ("DELETE" === actionToTake && (hasPermission(message.author.id, Permissions.MANAGE_MESSAGES, message.channel_id) || canEdit)) {
    return EditUtils.deleteMessage(message.channel_id, message.id);
  }
  if ("EDIT" === actionToTake && canEdit) {
    return EditUtils.startEditMessageRecord(message.channel_id, message, { shiftKey: false });
  }
  if ("REPLY" === actionToTake) {
    return ReplyAction(ChannelStore.getChannel(message.channel_id), message, { shiftKey: false });
  }
  if ("REACT" === actionToTake) {
    return addReaction(
      message.channel_id,
      message.id,
      doubleClickEmoji,
      "Message",
      {
        burst: MoreDoubleClickStore.getSetting("shouldEmojiBurst")
      }
    );
  }
  if ("NONE" === actionToTake) {
    return;
  }
}
function SettingsPanel() {
  const {
    emoji,
    guild,
    normalAction,
    shiftAction,
    ctrlAction,
    delAction,
    shouldBurst,
    textOverride
  } = useStateFromStores(MoreDoubleClickStore, () => ({
    emoji: MoreDoubleClickStore.getSetting("doubleClickEmoji"),
    guild: MoreDoubleClickStore.getSetting("selectedGuildForReaction"),
    normalAction: MoreDoubleClickStore.getSetting("normalDoubleClickAction"),
    shiftAction: MoreDoubleClickStore.getSetting("shiftDoubleClickAction"),
    ctrlAction: MoreDoubleClickStore.getSetting("ctrlDoubleClickAction"),
    delAction: MoreDoubleClickStore.getSetting("delDoubleClickAction"),
    shouldBurst: MoreDoubleClickStore.getSetting("shouldEmojiBurst"),
    textOverride: MoreDoubleClickStore.getSetting("textOverride")
  }));
  const actionOptions = [
    { label: "None", value: "NONE" },
    { label: "Edit", value: "EDIT" },
    { label: "Reply", value: "REPLY" },
    { label: "React", value: "REACT" },
    { label: "Delete", value: "DELETE" }
  ];
  const setNewEmoji = (emoji2) => {
    const newEmoji = {
      id: emoji2.id || null,
      isGuildEmoji: emoji2.id != null,
      name: emoji2.surrogates ?? emoji2.name,
      animated: emoji2.animated,
      icon: emoji2.id ? `https://cdn.discordapp.com/emojis/${emoji2.id}.${emoji2.animated ? "gif" : "webp"}?size=32` : null
    };
    MoreDoubleClickStore.setSetting("doubleClickEmoji", newEmoji);
  };
  const guildMapping = GuildStore.getGuildsArray().map((x) => {
    return {
      label: /* @__PURE__ */ BdApi.React.createElement("div", { style: {
        display: "flex",
        gap: "10px",
        alignItems: "center"
      } }, /* @__PURE__ */ BdApi.React.createElement(
        "img",
        {
          src: `https://cdn.discordapp.com/icons/${x.id}/${x.icon}.png?size=32`,
          style: { width: "20px", height: "20px", borderRadius: "50%" }
        }
      ), x.name),
      value: x.id
    };
  });
  guildMapping.unshift({ label: "Default", value: "0" });
  return /* @__PURE__ */ BdApi.React.createElement("div", { style: { minHeight: "500px", padding: "10px" } }, /* @__PURE__ */ BdApi.React.createElement(Components.SettingItem, { id: "normalAction", name: "Normal Double-Click", inline: true }, /* @__PURE__ */ BdApi.React.createElement(
    Components.DropdownInput,
    {
      value: normalAction,
      onChange: (e) => MoreDoubleClickStore.setSetting("normalDoubleClickAction", e),
      options: actionOptions
    }
  )), /* @__PURE__ */ BdApi.React.createElement(Components.SettingItem, { id: "shiftAction", name: "Shift + Double-Click", inline: true }, /* @__PURE__ */ BdApi.React.createElement(
    Components.DropdownInput,
    {
      value: shiftAction,
      onChange: (e) => MoreDoubleClickStore.setSetting("shiftDoubleClickAction", e),
      options: actionOptions
    }
  )), /* @__PURE__ */ BdApi.React.createElement(Components.SettingItem, { id: "ctrlAction", name: "Ctrl/Cmd + Double-Click", inline: true }, /* @__PURE__ */ BdApi.React.createElement(
    Components.DropdownInput,
    {
      value: ctrlAction,
      onChange: (e) => MoreDoubleClickStore.setSetting("ctrlDoubleClickAction", e),
      options: actionOptions
    }
  )), /* @__PURE__ */ BdApi.React.createElement(Components.SettingItem, { id: "delAction", name: "DEL + Double-Click", inline: true }, /* @__PURE__ */ BdApi.React.createElement(
    Components.DropdownInput,
    {
      value: delAction,
      onChange: (e) => MoreDoubleClickStore.setSetting("delDoubleClickAction", e),
      options: actionOptions
    }
  )), /* @__PURE__ */ BdApi.React.createElement(
    Components.SettingItem,
    {
      id: "shouldBurst",
      name: "Use Burst Reaction",
      note: "Enable burst/super reactions",
      inline: true
    },
    /* @__PURE__ */ BdApi.React.createElement(
      Components.SwitchInput,
      {
        value: shouldBurst,
        onChange: (v) => MoreDoubleClickStore.setSetting("shouldEmojiBurst", v)
      }
    )
  ), /* @__PURE__ */ BdApi.React.createElement(
    Components.SettingItem,
    {
      id: "textOverride",
      name: "Allow double click on text",
      note: "Allows double clicks to trigger when double clicking/selecting text.",
      inline: true
    },
    /* @__PURE__ */ BdApi.React.createElement(
      Components.SwitchInput,
      {
        value: textOverride,
        onChange: (v) => MoreDoubleClickStore.setSetting("textOverride", v)
      }
    )
  ), /* @__PURE__ */ BdApi.React.createElement(Components.SettingItem, { id: "selectedEmoji", name: "Currently Selected Emoji", inline: true }, emoji?.isGuildEmoji ? /* @__PURE__ */ BdApi.React.createElement("img", { src: emoji.icon, style: { width: "32px", height: "32px" } }) : /* @__PURE__ */ BdApi.React.createElement("span", { style: { fontSize: "32px" } }, emoji?.name)), /* @__PURE__ */ BdApi.React.createElement(Components.SettingItem, { id: "selectedGuild", name: "Select Guild for Emojis", inline: true }, /* @__PURE__ */ BdApi.React.createElement(
    Components.DropdownInput,
    {
      value: GuildStore.getGuild(guild)?.id,
      onChange: (e) => MoreDoubleClickStore.setSetting("selectedGuildForReaction", e),
      options: guildMapping
    }
  )), /* @__PURE__ */ BdApi.React.createElement(Components.SettingItem, { id: "emojiGrid", name: "Select Emoji" }, /* @__PURE__ */ BdApi.React.createElement("div", { style: {
    display: "grid",
    gridTemplateColumns: "repeat(auto-fill, minmax(40px, 1fr))",
    gap: "5px",
    marginTop: "10px"
  } }, guild != 0 ? Object.values(RawGuildEmojiStore.getGuildEmojis(guild) ?? {}).filter((x) => x?.id).map(
    (x) => /* @__PURE__ */ BdApi.React.createElement(
      "img",
      {
        key: x.id,
        onClick: () => setNewEmoji(x),
        src: `https://cdn.discordapp.com/emojis/${x.id}.${x.animated ? "gif" : "webp"}?size=80`,
        style: { width: "40px", height: "40px", cursor: "pointer", borderRadius: "4px" },
        title: x.name
      }
    )
  ) : Object.values(EmojiPack()).map(
    (x) => /* @__PURE__ */ BdApi.React.createElement(
      "div",
      {
        key: String(x.names).split(" ").join(", "),
        onClick: () => setNewEmoji(x),
        style: {
          width: "40px",
          height: "40px",
          fontSize: "40px",
          cursor: "pointer",
          textAlign: "center"
        }
      },
      x.surrogates
    )
  ))));
}
var MoreDoubleClicks = class {
  async start() {
    DataStore.settings = {
      normalDoubleClickAction: "REPLY",
      shiftDoubleClickAction: "EDIT",
      ctrlDoubleClickAction: "REACT",
      delDoubleClickAction: "DELETE",
      selectedGuildForReaction: Object.values(GuildStore.getGuilds())[0].id,
      // id or 0
      doubleClickEmoji: {
        "id": null,
        "name": "\u{1F62D}",
        "animated": false
      },
      shouldEmojiBurst: false,
      textOverride: true,
      ...DataStore.settings || {}
    };
    this.handleKeyDown = (e) => {
      if (e.key === "Delete") {
        MoreDoubleClickStore.setDeleteKeyPressed(true);
      }
    };
    this.handleKeyUp = (e) => {
      if (e.key === "Delete") {
        MoreDoubleClickStore.setDeleteKeyPressed(false);
      }
    };
    document.addEventListener("keydown", this.handleKeyDown);
    document.addEventListener("keyup", this.handleKeyUp);
    const MessageContentA = Webpack.getModule(Webpack.Filters.bySource('location:"BaseMessage"'));
    Patcher.after(MessageContentA, "A", (_, args, ret) => {
      const target = Utils.findInTree(ret, (x) => x?.role, { walkable: ["props", "children"] });
      Object.defineProperty(target, "onDoubleClick", {
        value: (event) => {
          const message = args[0].childrenMessageContent.props.children.props.message;
          StartDoubleClickAction(_, args, ret, event, message);
        },
        configurable: true,
        enumerable: true
      });
    });
  }
  getSettingsPanel() {
    return /* @__PURE__ */ BdApi.React.createElement(SettingsPanel, null);
  }
  stop() {
    if (this.handleKeyDown) {
      document.removeEventListener("keydown", this.handleKeyDown);
    }
    if (this.handleKeyUp) {
      document.removeEventListener("keyup", this.handleKeyUp);
    }
    Patcher.unpatchAll();
  }
};
