var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _DevvitBlocksWebView_iframe, _DevvitBlocksWebView_handleMessage, _DevvitBlocksWebView_sendStateUpdate;
import { customEvent } from '@reddit/faceplate/lib/custom-event.js';
import { css, html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { createRef, ref } from 'lit/directives/ref.js';
/**
 * Wraps an iframe to capture messages sent from within to be handled by the app rendering the Blocks
 */
let DevvitBlocksWebView = class DevvitBlocksWebView extends LitElement {
    constructor() {
        super(...arguments);
        this.originDiffers = false;
        _DevvitBlocksWebView_iframe.set(this, createRef());
        _DevvitBlocksWebView_handleMessage.set(this, (event) => {
            if (event.source === __classPrivateFieldGet(this, _DevvitBlocksWebView_iframe, "f").value?.contentWindow) {
                event.preventDefault();
                event.stopImmediatePropagation();
                this.dispatchEvent(customEvent('message', event.data));
            }
        });
        _DevvitBlocksWebView_sendStateUpdate.set(this, () => {
            __classPrivateFieldGet(this, _DevvitBlocksWebView_iframe, "f")?.value?.contentWindow?.postMessage({
                type: 'stateUpdate',
                data: this.state,
            }, '*');
        });
    }
    static get styles() {
        return [
            css `
        :host {
          display: flex;
          flex-direction: row;
        }

        iframe {
          flex-grow: 1;
          border: 0;
          max-width: 100%;
          max-height: 100%;
        }
      `,
        ];
    }
    connectedCallback() {
        super.connectedCallback();
        window.addEventListener('message', __classPrivateFieldGet(this, _DevvitBlocksWebView_handleMessage, "f"));
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        window.removeEventListener('message', __classPrivateFieldGet(this, _DevvitBlocksWebView_handleMessage, "f"));
    }
    willUpdate(changedProperties) {
        if (changedProperties.has('state')) {
            __classPrivateFieldGet(this, _DevvitBlocksWebView_sendStateUpdate, "f").call(this);
        }
        if (changedProperties.has('src') && this.src) {
            const url = new URL(this.src);
            this.originDiffers = url.host !== location.host;
        }
    }
    render() {
        // Only allow the page to run in `same origin` mode if it's different from ours
        const allowOrigin = this.originDiffers ? 'allow-same-origin' : '';
        return html `<iframe
      src="${ifDefined(this.src)}"
      sandbox="allow-scripts ${allowOrigin}"
      referrerpolicy="origin"
      @load="${__classPrivateFieldGet(this, _DevvitBlocksWebView_sendStateUpdate, "f")}"
      ${ref(__classPrivateFieldGet(this, _DevvitBlocksWebView_iframe, "f"))}
    ></iframe>`;
    }
};
_DevvitBlocksWebView_iframe = new WeakMap();
_DevvitBlocksWebView_handleMessage = new WeakMap();
_DevvitBlocksWebView_sendStateUpdate = new WeakMap();
__decorate([
    property({ type: String }),
    __metadata("design:type", Object)
], DevvitBlocksWebView.prototype, "src", void 0);
__decorate([
    property({ attribute: false }),
    __metadata("design:type", Object)
], DevvitBlocksWebView.prototype, "state", void 0);
__decorate([
    state(),
    __metadata("design:type", Boolean)
], DevvitBlocksWebView.prototype, "originDiffers", void 0);
DevvitBlocksWebView = __decorate([
    customElement('devvit-blocks-webview')
], DevvitBlocksWebView);
export { DevvitBlocksWebView };
//# sourceMappingURL=devvit-blocks-webview.js.map