'use strict';

var _package = require('./package-YyJzVqrh.js');
var Api = require('./Api-D9vOcUw4.js');
require('./Plaid-CPESyiiV.js');
require('./Pagination-Dediz133.js');
require('./Business-oBifyXkW.js');

const api = Api.Api();
class AnalyticsService {
    async record(body) {
        const endpoint = 'analytics';
        return api.post({ endpoint, body });
    }
}

/**
 * @class JustifiAnalytics
 * @description A class to track analytics for a component
 * @param { component } ComponentInterface - The component to track
 *
 */
class JustifiAnalytics {
    constructor(component) {
        this.eventListeners = new Map();
        this.handleCustomEvent = async (data) => {
            // Track the event
            await this.service.record(data);
        };
        // dont track analytics on local dev or public docs site
        if (window.location.origin.includes('localhost') ||
            window.location.origin.includes('docs.justifi.tech')) {
            return;
        }
        this.service = new AnalyticsService();
        this.componentInstance = component;
        this.setUpBasicData();
        this.setupLifecycleTracking();
        this.trackCustomEvents();
    }
    get resourceId() {
        return (this.componentInstance.accountId ||
            this.componentInstance.businessId ||
            this.componentInstance.paymentId ||
            this.componentInstance.payoutId ||
            this.componentInstance.checkoutId);
    }
    setUpBasicData() {
        this.basicData = {
            component_name: this.componentInstance.tagName,
            component_version: _package.packageJson.version,
            client_user_agent: navigator.userAgent,
            // navigator.platform is deprecated, use navigator.userAgent instead
            client_platform: navigator.userAgent,
            client_origin: window.location.origin,
            resource_id: this.resourceId,
        };
    }
    trackCustomEvents() {
        this.eventEmitters = ['submit-event', 'error-event'];
        // for each event, add an event listener
        this.eventEmitters.forEach((eventName) => {
            // if this.componentInstance.addEventListener is a function add the event listener
            if (typeof this.componentInstance.addEventListener === 'function') {
                const boundHandler = (event) => this.handleCustomEvent({
                    event_type: eventName,
                    data: Object.assign(Object.assign({}, this.basicData), (eventName === 'error-event' && { error: event.detail })),
                });
                this.eventListeners.set(eventName, boundHandler);
                this.componentInstance.addEventListener(eventName, boundHandler);
            }
        });
    }
    setupLifecycleTracking() {
        const originalComponentDidLoad = this.componentInstance.componentDidLoad;
        this.componentInstance.componentDidLoad = () => {
            this.service.record({ event_type: 'init', data: this.basicData });
            if (originalComponentDidLoad) {
                return originalComponentDidLoad.apply(this.componentInstance);
            }
        };
    }
    cleanup() {
        // Remove event listeners
        this.eventListeners.forEach((handler, eventName) => {
            this.componentInstance.removeEventListener(eventName, handler);
        });
        this.eventListeners.clear();
    }
}

exports.JustifiAnalytics = JustifiAnalytics;
