/**
 * @name Quoter
 * @description Right click a message to quote your friends wild statements.
 * @author Kaan
 * @version 1.0.6
 * @source https://github.com/zrodevkaan/BDPlugins/tree/main/Plugins/Quoter/Quoter.plugin.js
 * @invite t3zMgv7Nvb
 * @stable 627798
 * @canary 628758
 */
"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/Quoter/index.tsx
var index_exports = {};
__export(index_exports, {
  default: () => Quoter,
  timestampToSnowflake: () => timestampToSnowflake
});
module.exports = __toCommonJS(index_exports);
var { Webpack, Patcher, ContextMenu, Data, Components, Utils } = new BdApi("Quoter");
function calculateFontSize({
  charCount,
  width,
  height
}) {
  let baseSize;
  if (charCount <= 20) {
    baseSize = 48;
  } else if (charCount <= 50) {
    baseSize = 36;
  } else if (charCount <= 100) {
    baseSize = 28;
  } else if (charCount <= 200) {
    baseSize = 22;
  } else {
    baseSize = 18;
  }
  const estimatedCharWidth = baseSize * 0.6;
  const charsPerLine = Math.floor(width / estimatedCharWidth);
  const estimatedLines = Math.ceil(charCount / charsPerLine);
  const requiredHeight = estimatedLines * baseSize * 1.2;
  if (requiredHeight > height * 0.8) {
    baseSize = baseSize * (height * 0.8) / requiredHeight;
  }
  return Math.max(16, Math.min(baseSize, 60));
}
var generateQuoteImage = async (imageUrl, text, attribution, width = 1250, height = 530) => {
  return new Promise(async (resolve, reject) => {
    const canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    const ctx = canvas.getContext("2d");
    function wrapTextCentered(ctx2, text2, x, y, maxWidth, lineHeight) {
      var words = text2.split(" ");
      var lines = [];
      var line = "";
      for (var n = 0; n < words.length; n++) {
        var testLine = line + words[n] + " ";
        var metrics = ctx2.measureText(testLine);
        var testWidth = metrics.width;
        if (testWidth > maxWidth && n > 0) {
          lines.push(line.trim());
          line = words[n] + " ";
        } else {
          line = testLine;
        }
      }
      if (line.trim()) {
        lines.push(line.trim());
      }
      var totalHeight = lines.length * lineHeight;
      var startY = y - totalHeight / 2 + lineHeight;
      var currentY = startY;
      for (var i = 0; i < lines.length; i++) {
        var lineWidth = ctx2.measureText(lines[i]).width;
        var centeredX = x + (maxWidth - lineWidth) / 2;
        ctx2.fillText(lines[i], centeredX, currentY);
        currentY += lineHeight;
      }
      return currentY;
    }
    try {
      const res = await BdApi.Net.fetch(imageUrl);
      const dataA = await res.blob();
      const url = URL.createObjectURL(dataA);
      const img = new Image();
      img.onload = () => {
        try {
          URL.revokeObjectURL(url);
          ctx.drawImage(img, 0, 0, 600, height);
          const grad = ctx.createLinearGradient(0, 45, 530, 0);
          grad.addColorStop(0, "rgba(0, 0, 0, 0)");
          grad.addColorStop(1, "rgba(0, 0, 0, 1)");
          ctx.fillStyle = grad;
          ctx.fillRect(0, 0, width, height);
          const availableWidth = 400;
          const availableHeight = height;
          const fontSize = calculateFontSize({
            charCount: text.length,
            width: availableWidth,
            height: availableHeight
          });
          const lineHeight = fontSize * 1.2;
          ctx.fillStyle = "white";
          ctx.font = `bold ${fontSize}px Arial`;
          const centerX = 650;
          const centerY = height / 2;
          const endY = wrapTextCentered(ctx, text, centerX, centerY, availableWidth, lineHeight);
          ctx.fillStyle = "rgba(104, 104, 104, 1)";
          ctx.font = "italic 20px Arial";
          const attrWidth = ctx.measureText(attribution).width;
          const attrX = centerX + (availableWidth - attrWidth) / 2;
          ctx.fillText("- @" + attribution, attrX - 10, endY + 5);
          canvas.toBlob((blob) => {
            if (blob) {
              resolve(blob);
            } else {
              reject(new Error("Failed to create blob"));
            }
          }, "image/png");
        } catch (err) {
          reject(err);
        }
      };
      img.onerror = () => {
        URL.revokeObjectURL(url);
        reject(new Error("Failed to load image"));
      };
      img.crossOrigin = "anonymous";
      img.src = url;
    } catch (err) {
      reject(new Error("Failed to fetch image: " + err.message));
    }
  });
};
var CloudUploader = Webpack.getByStrings("uploadFileToCloud", { searchExports: true });
var SelectedStore = Webpack.getStore("SelectedChannelStore");
var UserStore = Webpack.getStore("UserStore");
var GuildMemberStore = Webpack.getStore("GuildMemberStore");
var SelectedGuildStore = Webpack.getStore("SelectedGuildStore");
var mods = Webpack.getByKeys("getSendMessageOptionsForReply");
var PendingReplyStore = Webpack.getStore("PendingReplyStore");
var FluxDispatcher = Webpack.getModule((x) => x._dispatch);
var timestampToSnowflake = (timestamp) => {
  const DISCORD_EPOCH = BigInt(14200704e5);
  const SHIFT = BigInt(22);
  const ms = BigInt(timestamp) - DISCORD_EPOCH;
  return ms <= BigInt(0) ? "0" : (ms << SHIFT).toString();
};
async function upload(a, b, c, channelId, messageId) {
  const yeah = await generateQuoteImage(a, b, c);
  const file = new File([yeah], "quote.png", { type: "image/png" });
  const upload2 = new CloudUploader({ file }, SelectedStore.getCurrentlySelectedChannelId());
  const messagePayload = {
    flags: 0,
    channel_id: channelId,
    content: "",
    sticker_ids: [],
    validNonShortcutEmojis: [],
    type: 0,
    ...SettingStore.getSetting("shouldReply") ? {
      messageReference: {
        message_id: messageId,
        channel_id: channelId,
        guild_id: SelectedGuildStore.getGuildId() ?? void 0
      }
    } : null,
    // messageReference: replyOptions?.messageReference || null,
    nonce: timestampToSnowflake(Date.now())
  };
  mods.sendMessage(channelId, messagePayload, null, {
    attachmentsToUpload: [upload2],
    onAttachmentUploadError: () => false,
    ...messagePayload
  });
}
var SettingStore = new class SS extends Utils.Store {
  #settings = Data.load("settings");
  getSetting(key) {
    return this.#settings[key];
  }
  setSetting(key, value) {
    this.#settings[key] = value;
    this.emitChange();
  }
}();
var Quoter = class {
  constructor() {
    this.contextMenuPatch = null;
    Data.save("settings", {
      username: 0,
      shouldReply: false,
      ...Data.load("settings")
    });
  }
  async start() {
    this.contextMenuPatch = ContextMenu.patch("message", (res, props) => {
      res.props.children.props.children.push(ContextMenu.buildItem({
        label: "Quote User",
        action: async () => {
          const yesImage = UserStore.getUser(props.message.author.id).getAvatarURL(null, 1 << 12);
          const img = yesImage;
          const text = props.message.content;
          const setting = SettingStore.getSetting("username");
          let attribution;
          if (setting == 0) {
            attribution = props.message.author.username;
          } else if (setting == 1) {
            attribution = UserStore.getUser(props.message.author.id).globalName;
          } else if (setting == 2) {
            const member = GuildMemberStore.getMember(SelectedGuildStore.getGuildId(), props.message.author.id);
            attribution = member.nick;
          } else {
            attribution = props.message.author.username;
          }
          await upload(img, text, attribution, props.message.channel_id, props.message.id);
        }
      }));
    });
  }
  getSettingsPanel() {
    return () => {
      return /* @__PURE__ */ BdApi.React.createElement("div", null, /* @__PURE__ */ BdApi.React.createElement(
        Components.SettingItem,
        {
          name: "Username Option",
          note: "Changes what name shows of a user under the quote."
        },
        /* @__PURE__ */ BdApi.React.createElement(Components.RadioInput, { defaultValue: SettingStore.getSetting("username"), onChange: (e) => {
          SettingStore.setSetting("username", e);
        }, options: [
          {
            value: 0,
            name: "Username"
          },
          {
            value: 1,
            name: "Global Username"
          },
          {
            value: 2,
            name: "Server Username"
          }
        ] })
      ), /* @__PURE__ */ BdApi.React.createElement(Components.SettingItem, { name: "Reply", note: "Should you reply to the selected message?" }, /* @__PURE__ */ BdApi.React.createElement(Components.SwitchInput, { checked: SettingStore.getSetting("shouldReply"), onChange: (e) => {
        SettingStore.setSetting("shouldReply", e);
      } })));
    };
  }
  stop() {
    Patcher.unpatchAll();
    if (this.contextMenuPatch) {
      this.contextMenuPatch();
      this.contextMenuPatch = null;
    }
  }
};
