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;
};
import { provide } from '@lit-labs/context';
import { LitElement, html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { appEntrypointFilename, compile, newTSEnv, setSource } from '../../bundler/compiler.js';
import { link } from '../../bundler/linker.js';
import { PenSave, loadPen, savePen } from '../pen-save.js';
import { penCtx } from './play-pen-context.js';
/** play-pen context provider. */
let PlayPenContextProvider = class PlayPenContextProvider extends LitElement {
    constructor() {
        super(...arguments);
        this.allowStorage = false;
        this.allowURL = false;
        this.env = newTSEnv();
        this.name = '';
    }
    connectedCallback() {
        super.connectedCallback();
        let pen;
        if (this.allowURL) {
            pen = loadPen(globalThis.location);
            if (pen)
                this.setTemplate(pen.src);
        }
        if (this.allowStorage)
            pen ?? (pen = loadPen(globalThis.localStorage));
        if (!pen)
            return;
        this.setSrc(pen.src);
        this.setName(pen.name);
    }
    render() {
        return html `<slot
      @play-pen-set-name=${(ev) => this.setName(ev.detail)}
      @play-pen-set-scheme=${(ev) => this.setScheme(ev.detail)}
      @play-pen-set-src=${(ev) => this.setSrc(ev.detail)}
      @play-pen-set-template=${(ev) => this.setTemplate(ev.detail)}
    />`;
    }
    /** Save to LocalStorage as allowed. */
    autoSave() {
        if (this.allowStorage)
            savePen(undefined, globalThis.localStorage, PenSave(this.name, this.src ?? ''));
    }
    /** Save to LocalStorage and URL as allowed. */
    save() {
        savePen(this.allowURL ? globalThis.location : undefined, this.allowStorage ? globalThis.localStorage : undefined, PenSave(this.name, this.src ?? ''));
    }
    setName(name) {
        this.name = name;
        this.autoSave();
    }
    setScheme(scheme) {
        this.scheme = scheme;
    }
    setSrc(src) {
        this.src = src;
        setSource(this.env, src);
        this.env.updateFile(appEntrypointFilename, src || ' '); // empty strings trigger file deletion!
        // Skip blank source.
        if (!/^\s*$/.test(src))
            this.bundle = link(compile(this.env));
        this.autoSave();
    }
    setTemplate(template) {
        this.template = template;
    }
};
__decorate([
    provide({ context: penCtx.allowStorage }),
    property({ attribute: 'allow-storage', type: Boolean })
], PlayPenContextProvider.prototype, "allowStorage", void 0);
__decorate([
    provide({ context: penCtx.allowURL }),
    property({ attribute: 'allow-url', type: Boolean })
], PlayPenContextProvider.prototype, "allowURL", void 0);
__decorate([
    provide({ context: penCtx.bundle }),
    property({ type: Object })
], PlayPenContextProvider.prototype, "bundle", void 0);
__decorate([
    provide({ context: penCtx.env }),
    property({ attribute: false })
], PlayPenContextProvider.prototype, "env", void 0);
__decorate([
    provide({ context: penCtx.name }),
    property()
], PlayPenContextProvider.prototype, "name", void 0);
__decorate([
    provide({ context: penCtx.scheme }),
    property()
], PlayPenContextProvider.prototype, "scheme", void 0);
__decorate([
    provide({ context: penCtx.src }),
    property()
], PlayPenContextProvider.prototype, "src", void 0);
__decorate([
    provide({ context: penCtx.template }),
    property()
], PlayPenContextProvider.prototype, "template", void 0);
PlayPenContextProvider = __decorate([
    customElement('play-pen-context-provider')
], PlayPenContextProvider);
export { PlayPenContextProvider };
