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 __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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _PlayPreview_meta;
import penWorker from '@devvit/previews/dist/pen.worker.min.js';
import { BrowserLiteClient } from '@devvit/runtime-lite/BrowserLiteClient.js';
import { consume } from '@lit-labs/context';
import { LitElement, css, html } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { Bubble } from '../bubble.js';
import { penCtx } from './play-pen-context.js';
import '@devvit/previews/dist/devvit-preview.js';
/** The rendered output of Devvit program under various parameters. */
let PlayPreview = class PlayPreview extends LitElement {
    constructor() {
        super(...arguments);
        this._client = new BrowserLiteClient(new Blob([penWorker], { type: 'text/javascript' }));
        _PlayPreview_meta.set(this, {
            'devvit-app-user': { values: ['t2_appuser'] },
            'devvit-subreddit': { values: ['t5_devvit'] },
            'devvit-user': { values: ['t2_user'] }
        });
    }
    static get styles() {
        return css `
      .preview {
        /* Hide overflow on corners */
        overflow: hidden;
        border-width: 1px;
        border-style: solid;
        border-color: var(--rpl-neutral-border);
        border-radius: 16px;
        min-height: 320px;
        box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.25);

        /* to-do: adjust these colors to the theme. */
        /* When the background is visible, the preview is loading. */
        background-color: #f8f9fa;
        background: #f8f9fa
          repeating-linear-gradient(
            -45deg,
            #f8f9fa,
            #f8f9fa 18px,
            #eaecf080 18px,
            #eaecf080 32px
          )
          0 / 200%;
      }
    `;
    }
    disconnectedCallback() {
        this._client.quit();
        super.disconnectedCallback();
    }
    render() {
        return html `<div class="preview">
      ${this.bundle &&
            html `<devvit-preview
        @devvit-ui-error=${(ev) => this.dispatchEvent(Bubble('execution-error', ev.detail))}
        .meta="${__classPrivateFieldGet(this, _PlayPreview_meta, "f")}"
        .client=${this._client}
        .scheme=${this.scheme}
      ></devvit-preview>`}
    </div>`;
    }
    async willUpdate(changedProperties) {
        if (changedProperties.has('bundle') && this.bundle) {
            this._client.quit();
            try {
                await this._client.loadBundle(this.bundle);
            }
            catch (err) {
                this.dispatchEvent(Bubble('execution-error', err));
            }
            // Re-render the preview.
            __classPrivateFieldSet(this, _PlayPreview_meta, { ...__classPrivateFieldGet(this, _PlayPreview_meta, "f") }, "f");
            this.requestUpdate();
        }
    }
};
_PlayPreview_meta = new WeakMap();
__decorate([
    consume({ context: penCtx.bundle, subscribe: true }),
    property({ attribute: false })
], PlayPreview.prototype, "bundle", void 0);
__decorate([
    consume({ context: penCtx.scheme, subscribe: true }),
    property({ attribute: false })
], PlayPreview.prototype, "scheme", void 0);
__decorate([
    state()
], PlayPreview.prototype, "_client", void 0);
PlayPreview = __decorate([
    customElement('play-preview')
], PlayPreview);
export { PlayPreview };
