"use strict";
class ActionFeed extends HTMLElement {
    constructor() {
        super();
        this.url = null;
        this.cache = true;
    }
    static get observedAttributes() {
        return ['data-url', 'data-cache'];
    }
    connectedCallback() {
        const mountPoint = document.createElement('div');
        mountPoint.className = 'list-feed list-feed-solid';
        //this.attachShadow({ mode: 'open' }).appendChild(mountPoint);
        this.appendChild(mountPoint);
        this.loadData((data) => {
            data.result.forEach((element) => this.makeItem(mountPoint, element));
        });
    }
    attributeChangedCallback(name, _oldValue, newValue) {
        switch (name) {
            case 'data-url':
                this.url = newValue;
                break;
            case 'data-cache':
                this.cache = newValue ? newValue === 'true' : true;
                break;
            default:
                break;
        }
    }
    loadData(callback) {
        if (!this.url) {
            throw 'url required';
        }
        const data = new FormData();
        if (!this.cache) {
            data.append('cache', Math.random().toString());
        }
        const http = new XMLHttpRequest();
        http.open('POST', this.url);
        http.send(data);
        http.onreadystatechange = function () {
            if (this.readyState === 4 && this.status === 200) {
                callback(JSON.parse(http.responseText));
            }
        };
    }
    makeItem(parentElement, attributes) {
        const item = document.createElement('div');
        item.className = 'list-feed-item';
        const itemText = document.createElement('div');
        itemText.innerHTML = attributes.text;
        item.appendChild(itemText);
        const itemDate = document.createElement('div');
        itemDate.className = 'text-muted';
        itemDate.innerHTML = attributes.date;
        item.appendChild(itemDate);
        parentElement.appendChild(item);
    }
}
customElements.define('sa-action-feed', ActionFeed);
