import { DEAD_CLICKS_ENABLED_SERVER_SIDE } from '../constants';
import { isBoolean, isObject } from '../utils/type-utils';
import { assignableWindow, document } from '../utils/globals';
import { createLogger } from '../utils/logger';
var logger = createLogger('[Dead Clicks]');
export var isDeadClicksEnabledForHeatmaps = function () {
return true;
};
export var isDeadClicksEnabledForAutocapture = function (instance) {
var _a;
var isRemoteEnabled = !!((_a = instance.instance.persistence) === null || _a === void 0 ? void 0 : _a.get_property(DEAD_CLICKS_ENABLED_SERVER_SIDE));
var clientConfig = instance.instance.config.capture_dead_clicks;
return isBoolean(clientConfig) ? clientConfig : isRemoteEnabled;
};
var DeadClicksAutocapture = /** @class */ (function () {
function DeadClicksAutocapture(instance, isEnabled, onCapture) {
this.instance = instance;
this.isEnabled = isEnabled;
this.onCapture = onCapture;
this.startIfEnabled();
}
Object.defineProperty(DeadClicksAutocapture.prototype, "lazyLoadedDeadClicksAutocapture", {
get: function () {
return this._lazyLoadedDeadClicksAutocapture;
},
enumerable: false,
configurable: true
});
DeadClicksAutocapture.prototype.onRemoteConfig = 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 () {
var _this = this;
if (this.isEnabled(this)) {
this.loadScript(function () {
_this.start();
});
}
};
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('failed to load script', err);
return;
}
cb();
});
};
DeadClicksAutocapture.prototype.start = function () {
var _a;
if (!document) {
logger.error('`document` not found. Cannot start.');
return;
}
if (!this._lazyLoadedDeadClicksAutocapture &&
((_a = assignableWindow.__PosthogExtensions__) === null || _a === void 0 ? void 0 : _a.initDeadClicksAutocapture)) {
var config = isObject(this.instance.config.capture_dead_clicks)
? this.instance.config.capture_dead_clicks
: {};
config.__onCapture = this.onCapture;
this._lazyLoadedDeadClicksAutocapture = assignableWindow.__PosthogExtensions__.initDeadClicksAutocapture(this.instance, config);
this._lazyLoadedDeadClicksAutocapture.start(document);
logger.info("starting...");
}
};
DeadClicksAutocapture.prototype.stop = function () {
if (this._lazyLoadedDeadClicksAutocapture) {
this._lazyLoadedDeadClicksAutocapture.stop();
this._lazyLoadedDeadClicksAutocapture = undefined;
logger.info("stopping...");
}
};
return DeadClicksAutocapture;
}());
export { DeadClicksAutocapture };
//# sourceMappingURL=dead-clicks-autocapture.js.map