var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { r as registerInstance, h, g as getElement } from './chunk-82a2445c.js';
import './chunk-d7ac2f15.js';
import { U as Util } from './chunk-18ca1c85.js';
var Debugger = /** @class */ (function () {
    function Debugger(hostRef) {
        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;
    }
    Debugger.prototype.componentDidLoad = function () {
        var _this = this;
        var event = new CustomEvent("initDebugger", {
            detail: {}
        });
        window.dispatchEvent(event);
        document.onkeydown = function (e) { return _this.handleKeyDown(e); };
    };
    Debugger.prototype.setCurrentItem = function (index, addToHistory) {
        if (addToHistory === void 0) { 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."
        //         }
        //      }
        // }
    };
    Debugger.prototype.onListItemClick = function (index, addToHistory) {
        if (addToHistory === void 0) { addToHistory = false; }
        var divs = document.querySelectorAll('.url-list li');
        this.setCurrentItem(index, addToHistory);
    };
    Debugger.prototype.handleKeyDown = function (e) {
        var ctrlPressed;
        var altPressed;
        var shiftPressed;
        var 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;
    };
    Debugger.prototype.pushHistory = function (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);
    };
    Debugger.prototype.open = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                this.host.style.display = 'block';
                this.isOpen = true;
                return [2 /*return*/];
            });
        });
    };
    Debugger.prototype.close = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                this.host.style.display = 'none';
                this.isOpen = false;
                return [2 /*return*/];
            });
        });
    };
    Debugger.prototype.toggle = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                this.isOpen ? this.close() : this.open();
                return [2 /*return*/];
            });
        });
    };
    Debugger.prototype.run = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                this.addToHistory = true;
                this.isRunning = true;
                return [2 /*return*/];
            });
        });
    };
    Debugger.prototype.pause = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                this.addToHistory = false;
                this.isRunning = false;
                return [2 /*return*/];
            });
        });
    };
    Debugger.prototype.render = function () {
        var _this = this;
        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) {
                Util.fetchJson(this.jsonUrl).then(function (res) {
                    _this.data = res;
                    _this.addToHistory ? _this.pushHistory(_this.data) : _this.addToHistory = false;
                    _this.jsonLoaded = true;
                });
            }
        }
        return (h("div", null, h("ema-card", null, h("ema-card-header", null, h("span", { class: "ema-card-header-title" }, "Debugger: ", this.type), h("span", { class: "ema-card-header-subtitle" }, "Api url = ", this.apiUrl), h("div", { class: 'btn-close', onClick: function () { return _this.close(); } }, h("svg", { version: "1.1", xmlns: "http://www.w3.org/2000/svg", width: "22", height: "28", viewBox: "0 0 22 28" }, h("title", null, "close"), 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 ? (h("div", { class: 'btn-pause', onClick: function () { return _this.pause(); } }, h("svg", { version: "1.1", xmlns: "http://www.w3.org/2000/svg", width: "22", height: "22", viewBox: "0 0 22 22" }, h("title", null, "pause"), h("path", { d: "M2.75 2.75h6.875v16.5h-6.875zM12.375 2.75h6.875v16.5h-6.875z" })))) : (h("div", { class: 'btn-play', onClick: function () { return _this.run(); } }, h("svg", { version: "1.1", xmlns: "http://www.w3.org/2000/svg", width: "22", height: "22", viewBox: "0 0 22 22" }, h("title", null, "play"), h("path", { d: "M4.125 2.75l13.75 8.25-13.75 8.25z" }))))), h("ema-card-content", { class: 'url-list' }, this.type === 'dwp' &&
            h("ul", null, this.history.map(function (item, i) { return h("li", { "data-index": i, onClick: function (e) { return _this.onListItemClick(i, false); } }, item.type + ': ' + item.config.url.replace(_this.apiUrl, '')); }))), h("ema-card-footer", null, h("ema-collapsible", { label: this.viewerLabel, collapsed: false }, h("div", null, this.currentItem && !this.currentItem.response && // request
            h("div", null, h("div", null, "request: ", this.currentItem.config.method), 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
            h("div", null, h("div", null, "response:"), h("ema-json-viewer", { "data-json": JSON.stringify(this.currentItem.response), "allow-copy": 'true', "max-level": this.maxLevel || -1, "collapse-at": this.collapseAt || -1 }))))))));
    };
    Object.defineProperty(Debugger.prototype, "host", {
        get: function () { return getElement(this); },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Debugger, "style", {
        get: function () { 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%}"; },
        enumerable: true,
        configurable: true
    });
    return Debugger;
}());
export { Debugger as ema_debugger };
