'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _plugin = require('@swup/plugin');

var _plugin2 = _interopRequireDefault(_plugin);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var DebugPlugin = function (_Plugin) {
    _inherits(DebugPlugin, _Plugin);

    function DebugPlugin() {
        _classCallCheck(this, DebugPlugin);

        var _this = _possibleConstructorReturn(this, (DebugPlugin.__proto__ || Object.getPrototypeOf(DebugPlugin)).call(this));

        _this.name = "DebugPlugin";

        _this.triggerEvent = function (eventName, originalEvent) {
            if (originalEvent) {
                console.groupCollapsed('%cswup:' + '%c' + eventName, 'color: #343434', 'color: #009ACD');
                console.log(originalEvent);
                console.groupEnd();
            } else {
                console.log('%cswup:' + '%c' + eventName, 'color: #343434', 'color: #009ACD');
            }

            _this.swup._triggerEvent(eventName, originalEvent);
        };

        _this.log = function (str, object) {
            if (object) {
                console.groupCollapsed(str);
                for (var key in object) {
                    console.log(object[key]);
                }
                console.groupEnd();
            } else {
                console.log(str + '%c', 'color: #009ACD');
            }
        };

        _this.debugLog = function (log, type) {
            if (type === 'error') {
                console.error('DEBUG PLUGIN: ' + log);
            } else {
                console.warn('DEBUG PLUGIN: ' + log);
            }
        };

        if (!document.getElementsByTagName('title').length) {
            var error = "This page doesn't have title tag. Title tag is required in every page.";
            console.warn('DEBUG PLUGIN: ' + error);
        }
        return _this;
    }

    _createClass(DebugPlugin, [{
        key: 'mount',
        value: function mount() {
            var swup = this.swup;

            // set non-empty log method of swup
            swup.log = this.log;

            // set swup instance as a global variable swup
            window.swup = swup;

            // make events appear in console
            swup._triggerEvent = swup.triggerEvent;
            swup.triggerEvent = this.triggerEvent;

            // detect relative links not starting with / or #
            var potentiallyWrongLinksSelector = 'a[href]:not([href^="' + window.location.origin + '"]):not([href^="/"]):not([href^="http"]):not([href^="/"]):not([href^="?"]):not([href^="#"])';

            swup.on('pageView', function () {
                if (document.querySelectorAll(potentiallyWrongLinksSelector).length) {
                    var error = 'It seems there are some links with a href attribute not starting with "#", "/" or current domain, which is potentially a problem.';
                    console.warn('DEBUG PLUGIN: ' + error, document.querySelectorAll(potentiallyWrongLinksSelector));
                }
                if (document.querySelectorAll(potentiallyWrongLinksSelector).length) {
                    var _error = 'It seems there are some links with a href attribute not starting with "#", "/" or current domain, which is potentially a problem.';
                    console.warn('DEBUG PLUGIN: ' + _error, document.querySelectorAll(potentiallyWrongLinksSelector));
                }
            });
        }
    }, {
        key: 'unmount',
        value: function unmount() {
            this.swup.log = function () {};
            this.swup.triggerEvent = this.swup._triggerEvent;
        }
    }]);

    return DebugPlugin;
}(_plugin2.default);

exports.default = DebugPlugin;