import { DEAD_CLICKS_ENABLED_SERVER_SIDE } from '../constants';
import { isBoolean, isObject } from '../utils/type-utils';
import { assignableWindow, document } from '../utils/globals';
import { logger } from '../utils/logger';
var LOGGER_PREFIX = '[Dead Clicks]';
var DeadClicksAutocapture = /** @class */ (function () {
    function DeadClicksAutocapture(instance) {
        this.instance = instance;
        this.startIfEnabled();
    }
    Object.defineProperty(DeadClicksAutocapture.prototype, "lazyLoadedDeadClicksAutocapture", {
        get: function () {
            return this._lazyLoadedDeadClicksAutocapture;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(DeadClicksAutocapture.prototype, "isRemoteEnabled", {
        get: function () {
            var _a;
            return !!((_a = this.instance.persistence) === null || _a === void 0 ? void 0 : _a.get_property(DEAD_CLICKS_ENABLED_SERVER_SIDE));
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(DeadClicksAutocapture.prototype, "isEnabled", {
        get: function () {
            var clientConfig = this.instance.config.capture_dead_clicks;
            return isBoolean(clientConfig) ? clientConfig : this.isRemoteEnabled;
        },
        enumerable: false,
        configurable: true
    });
    DeadClicksAutocapture.prototype.afterDecideResponse = function (response) {
        var _a;
        if (this.instance.persistence) {
            this.instance.persistence.register((_a = {},
                _a[DEAD_CLICKS_ENABLED_SERVER_SIDE] = response === null || response === void 0 ? void 0 : response.captureDeadClicks,
                _a));
        }
        this.startIfEnabled();
    };
    DeadClicksAutocapture.prototype.startIfEnabled = function () {
        if (this.isEnabled) {
            this.loadScript(this.start.bind(this));
        }
    };
    DeadClicksAutocapture.prototype.loadScript = function (cb) {
        var _a, _b, _c;
        if ((_a = assignableWindow.__PosthogExtensions__) === null || _a === void 0 ? void 0 : _a.initDeadClicksAutocapture) {
            // already loaded
            cb();
        }
        (_c = (_b = assignableWindow.__PosthogExtensions__) === null || _b === void 0 ? void 0 : _b.loadExternalDependency) === null || _c === void 0 ? void 0 : _c.call(_b, this.instance, 'dead-clicks-autocapture', function (err) {
            if (err) {
                logger.error(LOGGER_PREFIX + ' failed to load script', err);
                return;
            }
            cb();
        });
    };
    DeadClicksAutocapture.prototype.start = function () {
        var _a;
        if (!document) {
            logger.error(LOGGER_PREFIX + ' `document` not found. Cannot start.');
            return;
        }
        if (!this._lazyLoadedDeadClicksAutocapture &&
            ((_a = assignableWindow.__PosthogExtensions__) === null || _a === void 0 ? void 0 : _a.initDeadClicksAutocapture)) {
            this._lazyLoadedDeadClicksAutocapture = assignableWindow.__PosthogExtensions__.initDeadClicksAutocapture(this.instance, isObject(this.instance.config.capture_dead_clicks)
                ? this.instance.config.capture_dead_clicks
                : undefined);
            this._lazyLoadedDeadClicksAutocapture.start(document);
            logger.info("".concat(LOGGER_PREFIX, " starting..."));
        }
    };
    DeadClicksAutocapture.prototype.stop = function () {
        if (this._lazyLoadedDeadClicksAutocapture) {
            this._lazyLoadedDeadClicksAutocapture.stop();
            this._lazyLoadedDeadClicksAutocapture = undefined;
            logger.info("".concat(LOGGER_PREFIX, " stopping..."));
        }
    };
    return DeadClicksAutocapture;
}());
export { DeadClicksAutocapture };
//# sourceMappingURL=dead-clicks-autocapture.js.map