'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const __chunk_1 = require('./chunk-2755df65.js');
require('./chunk-f5b13333.js');
const __chunk_3 = require('./chunk-451ee4a2.js');

class Debugger {
    constructor(hostRef) {
        __chunk_1.registerInstance(this, hostRef);
        this.history = [];
        this.viewerLabel = 'Select a route to inspect';
        this.addToHistory = true;
        this.isActive = false;
        this.isOpen = false;
        this.isRunning = true;
        this.jsonLoaded = false;
        this.type = "uaas";
        this.historySize = 100;
    }
    componentDidLoad() {
        let event = new CustomEvent("initDebugger", {
            detail: {}
        });
        window.dispatchEvent(event);
        document.onkeydown = (e) => this.handleKeyDown(e);
    }
    setCurrentItem(index, addToHistory = true) {
        this.addToHistory = addToHistory;
        this.currentItem = this.history[index];
        if (this.type === 'dwp') {
            if (this.currentItem.response)
                this.currentItem.config = this.currentItem.response.config;
            this.viewerLabel = 'URL: ' + this.currentItem.config.url.replace(this.apiUrl, '');
        }
        if (this.type === 'uaas') {
            this.viewerLabel = 'URL: ' + this.currentItem.config.url.replace(this.apiUrl, '');
        }
        // Json-api
        // {
        //     "data": {
        //         "id": "5586c305-df09-4c1e-b987-40225ddb14fa",
        //         "type": "http--request",
        //         "client": "dwp",
        //         "attributes": {
        //             "uuid": "cb77be69-130f-42ff-9b6b-33989e0a0883",
        //             "ip_address": "237.20.30.226",
        //             "body": "Nullam porttitor lacus at turpis. Donec posuere metus vitae ipsum. Aliquam non mauris."
        //         }
        //      }
        // }
    }
    onListItemClick(index, addToHistory = false) {
        let divs = document.querySelectorAll('.url-list li');
        this.setCurrentItem(index, addToHistory);
    }
    handleKeyDown(e) {
        let ctrlPressed;
        let altPressed;
        let shiftPressed;
        let evt = (e == null ? event : e);
        shiftPressed = evt.shiftKey;
        altPressed = evt.altKey;
        ctrlPressed = evt.ctrlKey;
        if ((shiftPressed || altPressed || ctrlPressed)
            && (evt.keyCode < 16 || evt.keyCode > 18))
            if (ctrlPressed && evt.keyCode == 68) {
                this.toggle();
            }
        return true;
    }
    pushHistory(value) {
        if (this.history.length >= Number(this.historySize)) {
            this.history.pop();
        }
        this.history.unshift(value);
        // if (this.history.length >= Number(this.historySize)){
        //     this.history.shift();
        // }
        // this.history.push(value);
    }
    async open() {
        this.host.style.display = 'block';
        this.isOpen = true;
    }
    async close() {
        this.host.style.display = 'none';
        this.isOpen = false;
    }
    async toggle() {
        this.isOpen ? this.close() : this.open();
    }
    async run() {
        this.addToHistory = true;
        this.isRunning = true;
    }
    async pause() {
        this.addToHistory = false;
        this.isRunning = false;
    }
    render() {
        if (this.isRunning) {
            if (this.dataJson) {
                this.data = JSON.parse(this.dataJson);
                this.addToHistory ? this.pushHistory(this.data) : this.addToHistory = true;
            }
            if (this.jsonUrl && !this.jsonLoaded) {
                __chunk_3.Util.fetchJson(this.jsonUrl).then((res) => {
                    this.data = res;
                    this.addToHistory ? this.pushHistory(this.data) : this.addToHistory = false;
                    this.jsonLoaded = true;
                });
            }
        }
        return (__chunk_1.h("div", null, __chunk_1.h("ema-card", null, __chunk_1.h("ema-card-header", null, __chunk_1.h("span", { class: "ema-card-header-title" }, "Debugger: ", this.type), __chunk_1.h("span", { class: "ema-card-header-subtitle" }, "Api url = ", this.apiUrl), __chunk_1.h("div", { class: 'btn-close', onClick: () => this.close() }, __chunk_1.h("svg", { version: "1.1", xmlns: "http://www.w3.org/2000/svg", width: "22", height: "28", viewBox: "0 0 22 28" }, __chunk_1.h("title", null, "close"), __chunk_1.h("path", { d: "M20.281 20.656c0 0.391-0.156 0.781-0.438 1.062l-2.125 2.125c-0.281 0.281-0.672 0.438-1.062 0.438s-0.781-0.156-1.062-0.438l-4.594-4.594-4.594 4.594c-0.281 0.281-0.672 0.438-1.062 0.438s-0.781-0.156-1.062-0.438l-2.125-2.125c-0.281-0.281-0.438-0.672-0.438-1.062s0.156-0.781 0.438-1.062l4.594-4.594-4.594-4.594c-0.281-0.281-0.438-0.672-0.438-1.062s0.156-0.781 0.438-1.062l2.125-2.125c0.281-0.281 0.672-0.438 1.062-0.438s0.781 0.156 1.062 0.438l4.594 4.594 4.594-4.594c0.281-0.281 0.672-0.438 1.062-0.438s0.781 0.156 1.062 0.438l2.125 2.125c0.281 0.281 0.438 0.672 0.438 1.062s-0.156 0.781-0.438 1.062l-4.594 4.594 4.594 4.594c0.281 0.281 0.438 0.672 0.438 1.062z" }))), this.isRunning ? (__chunk_1.h("div", { class: 'btn-pause', onClick: () => this.pause() }, __chunk_1.h("svg", { version: "1.1", xmlns: "http://www.w3.org/2000/svg", width: "22", height: "22", viewBox: "0 0 22 22" }, __chunk_1.h("title", null, "pause"), __chunk_1.h("path", { d: "M2.75 2.75h6.875v16.5h-6.875zM12.375 2.75h6.875v16.5h-6.875z" })))) : (__chunk_1.h("div", { class: 'btn-play', onClick: () => this.run() }, __chunk_1.h("svg", { version: "1.1", xmlns: "http://www.w3.org/2000/svg", width: "22", height: "22", viewBox: "0 0 22 22" }, __chunk_1.h("title", null, "play"), __chunk_1.h("path", { d: "M4.125 2.75l13.75 8.25-13.75 8.25z" }))))), __chunk_1.h("ema-card-content", { class: 'url-list' }, this.type === 'dwp' &&
            __chunk_1.h("ul", null, this.history.map((item, i) => __chunk_1.h("li", { "data-index": i, onClick: (e) => this.onListItemClick(i, false) }, item.type + ': ' + item.config.url.replace(this.apiUrl, ''))))), __chunk_1.h("ema-card-footer", null, __chunk_1.h("ema-collapsible", { label: this.viewerLabel, collapsed: false }, __chunk_1.h("div", null, this.currentItem && !this.currentItem.response && // request
            __chunk_1.h("div", null, __chunk_1.h("div", null, "request: ", this.currentItem.config.method), __chunk_1.h("ema-json-viewer", { "data-json": JSON.stringify(this.currentItem.config), "allow-copy": 'true', "max-level": this.maxLevel || -1, "collapse-at": this.collapseAt || -1 })), this.currentItem && this.currentItem.response && // response
            __chunk_1.h("div", null, __chunk_1.h("div", null, "response:"), __chunk_1.h("ema-json-viewer", { "data-json": JSON.stringify(this.currentItem.response), "allow-copy": 'true', "max-level": this.maxLevel || -1, "collapse-at": this.collapseAt || -1 }))))))));
    }
    get host() { return __chunk_1.getElement(this); }
    static get style() { return ":root{--ema-color:#343a40;--ema-primary:#007bff;--ema-secondary:#6c757d;--ema-success:#28a745;--ema-info:#17a2b8;--ema-warning:#ffc107;--ema-danger:#dc3545;--ema-light:#f8f9fa;--ema-dark:#343a40;--ema-background:#fff;--ema-background-diap:rgba(29,29,29,0.97);--ema-border-color:#c5c9cd;--ema-border:1px solid #c5c9cd;--ema-border-radius:3px 3px 3px 3px;--ema-box-shadow:1px 1px 10px 1px #ccc;--ema-margin:0.25rem;--ema-margin-sm:0.5rem;--ema-margin-md:1rem;--ema-margin-lg:1.5rem;--ema-margin-xl:2rem;--ema-padding:0.25rem;--ema-padding-sm:0.5rem;--ema-padding-md:1rem;--ema-padding-lg:1.5rem;--ema-padding-xl:2rem;--ema-gray-100:#f8f9fa;--ema-gray-200:#e9ecef;--ema-gray-300:#dee2e6;--ema-gray-400:#ced4da;--ema-gray-500:#adb5bd;--ema-gray-600:#868e96;--ema-gray-700:#495057;--ema-gray-800:#343a40;--ema-gray-900:#212529}ema-debugger{display:none;border:var(--ema-border,#c5c9cd);position:absolute;top:55px;right:7.5px;min-width:50%;max-width:50%;z-index:9999}ema-debugger ema-card{background:var(--ema-background,#fff)!important}ema-debugger .btn-close{position:absolute;right:30px;top:11px;cursor:pointer}ema-debugger .btn-pause,ema-debugger .btn-play{position:absolute;right:70px;top:15px;cursor:pointer}ema-debugger ema-card-content.url-list ul{list-style-type:none;font-size:90%;padding-left:0;overflow-y:scroll;max-height:200px;min-height:200px;-webkit-box-shadow:var(--ema-box-shadow,1px 1px 10px 1px #ccc);box-shadow:var(--ema-box-shadow,1px 1px 10px 1px #ccc)}ema-debugger ema-card-content.url-list ul li{padding-left:var(--ema-padding-md,1rem);cursor:pointer}ema-debugger ema-card-content.url-list ul .active{background:#fff}ema-debugger ema-collapsible{display:block;-ms-transform:scale(.8);-webkit-transform:scale(.8);transform:scale(.8);-webkit-transform-origin:top left;transform-origin:top left;width:125%}"; }
}

exports.ema_debugger = Debugger;
