import { q as r, t, g as s, x, p as weStyles, i } from "./shared-styles-6f00e07f.js";
import { d as decodeHashFromBase64 } from "./base64-1426145d.js";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i2 = decorators.length - 1, decorator; i2 >= 0; i2--)
    if (decorator = decorators[i2])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result)
    __defProp(target, key, result);
  return result;
};
let WalWindow = class extends s {
  async firstUpdated() {
    window.addEventListener("message", async (message) => {
      const request = message.data.request;
      const handleRequest = async (request2) => {
        if (request2) {
          switch (request2.type) {
            case "sign-zome-call":
              return window.electronAPI.signZomeCallApplet(request2.request);
            case "user-select-screen":
              return window.electronAPI.selectScreenOrWindow();
            case "request-close":
              return window.electronAPI.closeWindow();
            case "user-select-wal": {
              await window.electronAPI.focusMainWindow();
              let error;
              let response;
              const appletToParentMessage2 = {
                request: message.data.request,
                appletHash: this.appletHash
              };
              try {
                response = await window.electronAPI.appletMessageToParent(
                  appletToParentMessage2
                );
              } catch (e) {
                error = e;
              }
              await window.electronAPI.focusMyWindow();
              if (error)
                return Promise.reject(`Failed to select WAL: ${error}`);
              return response;
            }
            default:
              const appletToParentMessage = {
                request: message.data.request,
                appletHash: this.appletHash
              };
              return window.electronAPI.appletMessageToParent(appletToParentMessage);
          }
        }
      };
      try {
        const result = await handleRequest(request);
        message.ports[0].postMessage({ type: "success", result });
      } catch (e) {
        console.error(
          "Error while handling applet iframe message. Error: ",
          e,
          "Message: ",
          message
        );
        message.ports[0].postMessage({ type: "error", error: e.message });
      }
    });
    const appletSrcInfo = await window.electronAPI.getMySrc();
    this.iframeSrc = appletSrcInfo.iframeSrc;
    this.appletHash = decodeHashFromBase64(appletSrcInfo.appletId);
    try {
      const appletInfo = await window.electronAPI.appletMessageToParent({
        request: {
          type: "get-applet-info",
          appletHash: this.appletHash
        },
        appletHash: this.appletHash
      });
      let assetLocationAndInfo;
      console.log("Getting global asset info for WAL: ", appletSrcInfo.wal);
      try {
        assetLocationAndInfo = await window.electronAPI.appletMessageToParent({
          request: {
            type: "get-global-asset-info",
            wal: appletSrcInfo.wal
          },
          appletHash: this.appletHash
        });
      } catch (e) {
        console.warn("Failed to get asset info: ", e);
      }
      let groupProfile;
      if (appletInfo.groupsHashes.length > 0) {
        const groupDnaHash = appletInfo.groupsHashes[0];
        try {
          groupProfile = await window.electronAPI.appletMessageToParent({
            request: {
              type: "get-group-profile",
              groupHash: groupDnaHash
            },
            appletHash: this.appletHash
          });
        } catch (e) {
          console.warn("Failed to get group profile: ", e);
        }
      }
      const title = `${appletInfo.appletName}${groupProfile ? ` (${groupProfile.name})` : ""} - ${assetLocationAndInfo ? `${assetLocationAndInfo.assetInfo.name}` : "unknown"}`;
      await window.electronAPI.setMyTitle(title);
      await window.electronAPI.setMyIcon(appletInfo.appletIcon);
    } catch (e) {
      console.warn("Failed to set window title or icon: ", e);
    }
  }
  render() {
    if (!this.iframeSrc)
      return x`<div class="center-content">Loading...</div>`;
    return x`
      <iframe
        frameborder="0"
        src="${this.iframeSrc}"
        style="flex: 1; display: block; padding: 0; margin: 0; height: 100vh;"
        allow="camera *; microphone *; clipboard-write *;"
      ></iframe>
    `;
  }
  static get styles() {
    return [
      weStyles,
      i`
        :host {
          flex: 1;
          display: flex;
          margin: 0;
          padding: 0;
        }
      `
    ];
  }
};
__decorateClass([
  r()
], WalWindow.prototype, "iframeSrc", 2);
__decorateClass([
  r()
], WalWindow.prototype, "appletHash", 2);
WalWindow = __decorateClass([
  t("wal-window")
], WalWindow);
