var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import Analytics, { dimensions, webxrDimensions, webxrEvents, events, fingerprint, } from '@r2u/analytics';
import { v4 } from 'uuid';
import state from '../state';
const placements = {
    arLink: 'ar_link',
    arModelViewer: 'ar_model_viewer',
    cta: 'cta',
};
const arLink = {
    componentDidMount: () => {
        return state.analytics.send({
            event: events.impression,
            data: { [dimensions.placement]: placements.arLink },
            scope: 'page',
        });
    },
    onClick: () => {
        return state.analytics.send({
            event: events.click,
            data: { [dimensions.placement]: placements.arLink },
        });
    },
    onFallback: () => {
        return state.analytics.send({
            event: events.fallback,
            data: { [dimensions.placement]: placements.arLink },
        });
    },
    onLike: () => {
        return state.analytics.send({
            event: events.like,
            data: { [dimensions.placement]: placements.arLink },
        });
    },
    onDislike: () => {
        return state.analytics.send({
            event: events.dislike,
            data: { [dimensions.placement]: placements.arLink },
        });
    },
    onSessionTimeCollect: (time) => {
        return state.analytics.send({
            event: events.interaction_duration,
            eventCount: time,
            data: { [dimensions.placement]: placements.arLink },
        });
    },
};
const arModelViewer = {
    componentDidMount: () => {
        return state.analytics.send({
            event: events.impression,
            data: { [dimensions.placement]: placements.arModelViewer },
            scope: 'page',
        });
    },
    onClick: () => {
        return state.analytics.send({
            event: events.click,
            data: { [dimensions.placement]: placements.arModelViewer },
        });
    },
    onSessionTimeCollect: (time) => {
        return state.analytics.send({
            event: events.interaction_duration,
            eventCount: time,
            data: { [dimensions.placement]: placements.arModelViewer },
        });
    },
};
const webXR = {
    onSessionStart: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.session_start,
        });
    },
    onSessionEnd: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.session_end,
        });
    },
    setSession: () => {
        state.analytics.webxrSet(webxrDimensions.session_id, v4());
    },
    setCompatible: (compatible) => {
        state.analytics.webxrSet(webxrDimensions.compatible, `${compatible}`);
    },
    onSceneViewerFallback: () => {
        return Promise.all([
            state.analytics.webxrSend({
                webxrEvent: webxrEvents.scene_viewer_fallback,
            }),
            state.analytics.send({
                event: events.fallback,
                data: { [dimensions.placement]: placements.arLink },
                context: 'webxr',
            }),
        ]);
    },
    onFallback: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.fallback,
        });
    },
    setPlaneMapped: () => {
        state.analytics.webxrSet(webxrDimensions.plane_mapped, new Date().toISOString());
    },
    setObjectPlaced: () => {
        state.analytics.webxrSet(webxrDimensions.object_placed, new Date().toISOString());
    },
    setTranslateInstructions: () => {
        state.analytics.webxrSet(webxrDimensions.translate_instructions, new Date().toISOString());
    },
    setRotateInstructions: () => {
        state.analytics.webxrSet(webxrDimensions.rotate_instructions, new Date().toISOString());
    },
    setFirstTranslation: () => {
        state.analytics.webxrSet(webxrDimensions.first_translation, new Date().toISOString());
    },
    setFirstRotation: () => {
        state.analytics.webxrSet(webxrDimensions.first_rotation, new Date().toISOString());
    },
    setReset: () => {
        state.analytics.webxrSet(webxrDimensions.reset, new Date().toISOString());
    },
    onShare: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.share,
        });
    },
    onExit: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.exit,
        });
    },
    onLike: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.like,
        });
    },
    onDislike: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.dislike,
        });
    },
    onCallToAction: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.call_to_action,
        });
    },
    onTranslation: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.translation,
        });
    },
    onRotation: () => {
        return state.analytics.webxrSend({
            webxrEvent: webxrEvents.rotation,
        });
    },
};
const cta = {
    onClick: () => {
        return state.analytics.send({
            event: events.click,
            data: { [dimensions.placement]: placements.cta },
            context: 'cart',
        });
    },
};
const setSku = (sku) => {
    state.analytics.set(dimensions.sku, sku);
};
const setManufacturerId = (manufacturerId) => {
    state.analytics.set(dimensions.manufacturer_id, manufacturerId);
};
const isActive3D = (isActive) => {
    state.analytics.set(dimensions.is_active_3d, `${isActive}`);
    state.analytics.send({
        event: events.product_page_view,
    });
};
const setExperimentId = (experimentId) => {
    state.analytics.set(dimensions.experiment_id, experimentId);
};
const setVariationId = (variationId) => {
    state.analytics.set(dimensions.variation_id, variationId);
};
const getFingerprint = () => __awaiter(void 0, void 0, void 0, function* () {
    const userFingerprint = yield fingerprint();
    state.analytics.set(dimensions.fingerprint, userFingerprint);
    return userFingerprint;
});
const getAnalytics = ({ client, dataLayerIntegration, sessionDurationMinutes, info = {}, }) => __awaiter(void 0, void 0, void 0, function* () {
    const params = {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        version: process.env.REACT_APP_VERSION,
        gcpAnalyticsUrl: 'https://us-central1-r2u-analytics.cloudfunctions.net/reportEvent',
        webxrGcpAnalyticsUrl: 'https://us-central1-r2u-analytics.cloudfunctions.net/reportWebXREvent',
        debug: !!new URLSearchParams(window.location.search).get('realidade-aumentada-debug'),
        sessionDurationMinutes,
        dataLayerIntegration,
    };
    const analytics = new Analytics(params, Object.assign(Object.assign({}, info), { [dimensions.client]: client }));
    yield analytics.init();
    return analytics;
});
export { arLink, arModelViewer, webXR, cta, setExperimentId, setVariationId, setSku, setManufacturerId, isActive3D, getFingerprint, };
export default getAnalytics;
//# sourceMappingURL=analytics.js.map