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, _PlayPreview_remote;
// @ts-expect-error
import * as sandboxedRuntimeScript from '@devvit/previews/dist/sandboxed-pen.worker.min.js';
// @ts-expect-error
import * as unsandboxedRuntimeScript from '@devvit/previews/dist/unsandboxed-pen.worker.min.js';
import {} from '@devvit/protos/lib/Types.js';
import {} from '@devvit/protos/types/google/protobuf/empty.js';
import { LitElement, css, html, nothing } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { RemoteApp } from '../runtime/remote-app.js';
import { defaultSettings } from '../storage/settings-save.js';
import { Bubble } from '../utils/bubble.js';
import { cssReset } from '../utils/css-reset.js';
import '@devvit/previews/dist/devvit-preview.js';
const sandboxedRuntimeBlob = new Blob([sandboxedRuntimeScript.default], {
    type: 'text/javascript'
});
const unsandboxedRuntimeBlob = new Blob([unsandboxedRuntimeScript.default], { type: 'text/javascript' });
/** The rendered output of Devvit program under various parameters. */
let PlayPreview = class PlayPreview extends LitElement {
    constructor() {
        super(...arguments);
        this.remoteRuntimeOrigin = defaultSettings.remoteRuntimeOrigin;
        this.runtimeDebugLogging = false;
        this.sandboxApp = false;
        this.useExperimentalBlocks = false;
        this.useLocalRuntime = false;
        this.useRemoteRuntime = false;
        this.useSpeculativeExecution = false;
        this.useUIRequest = false;
        this._err = false;
        _PlayPreview_meta.set(this, {
            'actor-id': { values: [] }, // Set in willUpdate().
            'devvit-accept-language': { values: [navigator.language] },
            'devvit-accept-timezone': {
                values: [Intl.DateTimeFormat().resolvedOptions().timeZone]
            },
            'devvit-actor': { values: ['main'] },
            'devvit-app': { values: ['pen'] },
            'devvit-app-user': { values: ['t2_123'] },
            'devvit-r2-host': { values: ['oauth.reddit.com'] },
            'devvit-installation': { values: ['123'] },
            'devvit-subreddit': { values: ['t5_123'] },
            'devvit-user': { values: ['t2_123'] },
            'devvit-user-agent': { values: ['Reddit;Play;not-provided'] }
        });
        _PlayPreview_remote.set(this, void 0);
    }
    render() {
        // to-do: don't override toaster's --rem16 to offset the toast. Upstream a
        // variable.
        return html `
      <div class="preview">
        ${this.bundle && !this._err
            ? html `
              <devvit-preview
                .bundle=${this.bundle}
                .localRuntimeCode=${this.useLocalRuntime
                ? this.sandboxApp
                    ? sandboxedRuntimeBlob
                    : unsandboxedRuntimeBlob
                : undefined}
                .metadata=${__classPrivateFieldGet(this, _PlayPreview_meta, "f")}
                .remote=${this.useRemoteRuntime ? __classPrivateFieldGet(this, _PlayPreview_remote, "f") : undefined}
                .scheme=${this.scheme}
                post-id="t3_123"
                ?runtime-debug-logging=${this.runtimeDebugLogging}
                style="--rem16: 50px;"
                ?use-experimental-blocks=${this.useExperimentalBlocks}
                ?use-ui-request=${this.useUIRequest}
                ?use-speculative-execution=${this.useSpeculativeExecution}
                @devvit-ui-error=${() => (this._err = true)}
                @devvit-image-upload=${mockImageUpload}
              ></devvit-preview>
            `
            : nothing}
      </div>
    `;
    }
    willUpdate(props) {
        super.willUpdate(props);
        if (props.has('bundle')) {
            this._err = false;
            if (this.bundle)
                __classPrivateFieldGet(this, _PlayPreview_meta, "f")['actor-id'].values = [this.bundle.hostname];
            this.dispatchEvent(Bubble('clear-errors', undefined));
        }
        if (props.has('remoteRuntimeOrigin'))
            __classPrivateFieldSet(this, _PlayPreview_remote, RemoteApp.new(this.remoteRuntimeOrigin, () => this.uploaded ?? Promise.resolve({}), __classPrivateFieldGet(this, _PlayPreview_meta, "f")), "f");
    }
};
_PlayPreview_meta = new WeakMap();
_PlayPreview_remote = new WeakMap();
PlayPreview.styles = css `
    ${cssReset}

    .preview {
      /* Hide overflow on corners */
      overflow: hidden;
      border-width: var(--border-width);
      border-style: solid;
      border-color: var(--color-neutral-border);
      border-radius: 16px;
      min-height: 320px;
      box-shadow: var(--shadow-xs);
      /* Prevents the border from throwing off the context.dimensions calculation */
      box-sizing: content-box;

      /* When the background is visible, the preview is loading. */
      background-color: var(--color-interactive-background);
      background-image: repeating-linear-gradient(
        -45deg,
        var(--color-interactive-background),
        var(--color-interactive-background) 18px,
        var(--color-interactive-background-disabled) 18px,
        var(--color-interactive-background-disabled) 32px
      );
      background-position-x: 0;
      background-position-y: 200%;
      min-width: 288px;
      transition-duration: 0.2s;
      transition-property: width;
      transition-timing-function: ease-out;
    }

    :host([previewWidth='288']) .preview {
      width: 288px;
    }
    :host([previewWidth='343']) .preview {
      width: 343px;
    }
    :host([previewWidth='400']) .preview {
      width: 400px;
    }
    :host([previewWidth='512']) .preview {
      width: 512px;
    }
    :host([previewWidth='718']) .preview {
      width: 718px;
    }
  `;
__decorate([
    property({ attribute: false })
], PlayPreview.prototype, "bundle", void 0);
__decorate([
    property({ type: Number })
], PlayPreview.prototype, "previewWidth", void 0);
__decorate([
    property({ attribute: 'remote-runtime-origin' })
], PlayPreview.prototype, "remoteRuntimeOrigin", void 0);
__decorate([
    property({ attribute: 'runtime-debug-logging', type: Boolean })
], PlayPreview.prototype, "runtimeDebugLogging", void 0);
__decorate([
    property({ attribute: 'sandbox-app', type: Boolean })
], PlayPreview.prototype, "sandboxApp", void 0);
__decorate([
    property()
], PlayPreview.prototype, "scheme", void 0);
__decorate([
    property({ attribute: false })
], PlayPreview.prototype, "uploaded", void 0);
__decorate([
    property({ attribute: 'use-experimental-blocks', type: Boolean })
], PlayPreview.prototype, "useExperimentalBlocks", void 0);
__decorate([
    property({ attribute: 'use-local-runtime', type: Boolean })
], PlayPreview.prototype, "useLocalRuntime", void 0);
__decorate([
    property({ attribute: 'use-remote-runtime', type: Boolean })
], PlayPreview.prototype, "useRemoteRuntime", void 0);
__decorate([
    property({ attribute: 'use-speculative-execution', type: Boolean })
], PlayPreview.prototype, "useSpeculativeExecution", void 0);
__decorate([
    property({ attribute: 'use-ui-request', type: Boolean })
], PlayPreview.prototype, "useUIRequest", void 0);
__decorate([
    state()
], PlayPreview.prototype, "_err", void 0);
PlayPreview = __decorate([
    customElement('play-preview')
], PlayPreview);
export { PlayPreview };
const mockImageUpload = (event) => {
    event.detail.uploadRequest = new Promise(resolve => {
        setTimeout(() => {
            resolve({
                url: 'https://i.redd.it/m04iwd26jbpc1.png',
                mediaId: 'm04iwd26jbpc1'
                // error:'Whoops!' // uncomment to emulate the error response
            });
        }, 2000);
    });
};
//# sourceMappingURL=play-preview.js.map