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);
};
import { html, css, property, } from 'lit-element';
import WebComponent from '../helpers/WebComponent';
export class Recomendation extends WebComponent {
    constructor() {
        super();
        this.currentPostId = '';
        this.currentContent = '';
        this.inputValue = '';
        this.loading = false;
        if (this.postId) {
            this.fetchPost(this.postId);
        }
    }
    set postId(value) {
        this.currentPostId = value;
        this.fetchPost(value);
    }
    get postId() {
        return this.currentPostId;
    }
    fetchPost(postId) {
        if (!postId) {
            this.loading = false;
            this.currentContent = '';
            this.requestUpdate();
            return;
        }
        // fetch from api
        this.loading = true;
        this.currentContent = '';
        this.requestUpdate();
        setTimeout(() => {
            if (postId === this.postId) {
                this.currentContent = `Статья «${postId}»`;
                this.loading = false;
                this.requestUpdate();
            }
        }, 2000);
    }
    createRenderRoot() {
        return this.attachShadow({
            mode: 'closed',
            // delegatesFocus: true,
        });
    }
    handleClick() {
        if (this.inputValue) {
            // this.postId = this.inputValue;
            this.fireChangeAttribute({ postid: this.inputValue });
            // this.requestUpdate();
            // this.fetchPost(this.postId);
        }
    }
    handleInput(e) {
        if (!e.currentTarget) {
            return;
        }
        const { value } = e.currentTarget;
        this.inputValue = value;
    }
    // Render element DOM by returning a `lit-html` template.
    render() {
        if (this.loading) {
            return html `<div>
        Грузится…
      </div>`;
        }
        let content;
        if (this.postId) {
            content = html `<div>
        <div>Вам рекомендуется почитать:</div>
        ${this.currentContent}
      </div>`;
        }
        else {
            content = html `<div>
        <input type="text" @input="${this.handleInput}" />
        <input type="button" @click="${this.handleClick}" value="Ок" />
      </div>`;
        }
        return html `<div>
      ${content}
    </div>`;
    }
}
Recomendation.styles = css `
    :host {
      background: lightgray;
      border: 1px solid red;
      display: block;
      padding: 1em;
      margin: 0 0 1em;
      user-select: none;
    }`;
__decorate([
    property({
        type: String,
        reflect: true,
    }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], Recomendation.prototype, "postId", null);
const defaultOptions = {
    name: 'exampleRecomendation',
    title: 'Example recomendation',
    tag: 'ARENA-RECOMENDATION',
    attributes: [],
    allowedAttributes: ['postid'],
    shortcut: 'Alt + KeyR',
    hint: 'r',
    command: 'add-recomendation',
    component: 'arena-recomendation',
    marks: [
        {
            tag: 'ARENA-RECOMENDATION',
            attributes: [],
        },
    ],
};
const examplePlugin = (opts) => ({
    register: (ta) => {
        const { name, icon, title, tag, attributes, allowedAttributes, shortcut, hint, command, component, marks, } = Object.assign(Object.assign({}, defaultOptions), (opts || {}));
        if (!customElements.get(component)) {
            customElements.define(component, Recomendation);
        }
        const arena = ta.registerArena({
            name,
            tag,
            attributes,
            allowedAttributes,
            single: true,
        }, marks, [ta.getRootArenaName()]);
        ta.registerCommand(command, (someTa, selection) => {
            const sel = someTa.insertBeforeSelected(selection, arena);
            return sel;
        });
        ta.registerShortcut(shortcut, command);
        ta.registerCreator({
            name,
            icon,
            title,
            shortcut,
            hint,
            command,
            canShow: (node) => node.parent.isAllowedNode(arena),
        });
    },
});
export default examplePlugin;
//# sourceMappingURL=examplePlugin.js.map