import { E as compilerRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { an as useLazyCryptQuery } from "./index-DfebxkL9.js";
const arrayBufferToString = (buf) => {
  const bytes = new Uint8Array(buf);
  return bytes.reduce((str, byte) => str + String.fromCharCode(byte), "");
};
const arrayBufferToBase64 = (encryptedBuf) => {
  return btoa(arrayBufferToString(encryptedBuf));
};
const stringToArrayBuffer = (str) => {
  const buf = new ArrayBuffer(str.length);
  const bufView = new Uint8Array(buf);
  for (let i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
};
const importKey = (jwk) => {
  return crypto.subtle.importKey("jwk", {
    kty: jwk.kty,
    e: jwk.e,
    n: jwk.n
  }, {
    name: "RSA-OAEP",
    hash: {
      name: "SHA-256"
    },
    length: 4096
  }, false, ["encrypt"]);
};
const encryptContent = async (jwk, content) => {
  const pubKey = await importKey(jwk);
  return crypto.subtle.encrypt({
    name: "RSA-OAEP"
  }, pubKey, stringToArrayBuffer(content));
};
const useFetchToCrypt = () => {
  const $ = compilerRuntimeExports.c(2);
  const [fetchCrypt] = useLazyCryptQuery();
  let t0;
  if ($[0] !== fetchCrypt) {
    t0 = async (message, succesResponse, errorResponse) => {
      const {
        data,
        error
      } = await fetchCrypt();
      const isError = error;
      if (!isError && data) {
        const encryptedBuf = await encryptContent(data.Jwk, message);
        const encryptedBase64 = arrayBufferToBase64(encryptedBuf);
        succesResponse(encryptedBase64);
      } else {
        errorResponse && errorResponse(new Error("Error"));
      }
    };
    $[0] = fetchCrypt;
    $[1] = t0;
  } else {
    t0 = $[1];
  }
  return t0;
};
const preserveCursorPosition = (ref, togglePasswordVisibility) => {
  if (ref.current) {
    const cursorPosition = ref.current.selectionStart;
    togglePasswordVisibility();
    setTimeout(() => {
      if (ref.current) {
        ref.current.focus();
        ref.current.setSelectionRange(cursorPosition, cursorPosition);
      }
    }, 0);
  }
};
export {
  preserveCursorPosition as p,
  useFetchToCrypt as u
};
//# sourceMappingURL=preserveCursorPosition-NFvD_VQV.js.map
