import { find } from './utils';
import { assignableWindow, navigator } from './utils/globals';
import { cookieStore, localStore } from './storage';
import { includes } from './utils/string-utils';
var OPT_OUT_PREFIX = '__ph_opt_in_out_';
export var ConsentStatus;
(function (ConsentStatus) {
ConsentStatus[ConsentStatus["PENDING"] = -1] = "PENDING";
ConsentStatus[ConsentStatus["DENIED"] = 0] = "DENIED";
ConsentStatus[ConsentStatus["GRANTED"] = 1] = "GRANTED";
})(ConsentStatus || (ConsentStatus = {}));
/**
* ConsentManager provides tools for managing user consent as configured by the application.
*/
var ConsentManager = /** @class */ (function () {
function ConsentManager(instance) {
this.instance = instance;
}
Object.defineProperty(ConsentManager.prototype, "config", {
get: function () {
return this.instance.config;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ConsentManager.prototype, "consent", {
get: function () {
if (this.getDnt()) {
return ConsentStatus.DENIED;
}
return this.storedConsent;
},
enumerable: false,
configurable: true
});
ConsentManager.prototype.isOptedOut = function () {
return (this.consent === ConsentStatus.DENIED ||
(this.consent === ConsentStatus.PENDING && this.config.opt_out_capturing_by_default));
};
ConsentManager.prototype.isOptedIn = function () {
return !this.isOptedOut();
};
ConsentManager.prototype.optInOut = function (isOptedIn) {
this.storage.set(this.storageKey, isOptedIn ? 1 : 0, this.config.cookie_expiration, this.config.cross_subdomain_cookie, this.config.secure_cookie);
};
ConsentManager.prototype.reset = function () {
this.storage.remove(this.storageKey, this.config.cross_subdomain_cookie);
};
Object.defineProperty(ConsentManager.prototype, "storageKey", {
get: function () {
var _a = this.instance.config, token = _a.token, opt_out_capturing_cookie_prefix = _a.opt_out_capturing_cookie_prefix;
return (opt_out_capturing_cookie_prefix || OPT_OUT_PREFIX) + token;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ConsentManager.prototype, "storedConsent", {
get: function () {
var value = this.storage.get(this.storageKey);
return value === '1' ? ConsentStatus.GRANTED : value === '0' ? ConsentStatus.DENIED : ConsentStatus.PENDING;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ConsentManager.prototype, "storage", {
get: function () {
if (!this._storage) {
var persistenceType = this.config.opt_out_capturing_persistence_type;
this._storage = persistenceType === 'localStorage' ? localStore : cookieStore;
var otherStorage = persistenceType === 'localStorage' ? cookieStore : localStore;
if (otherStorage.get(this.storageKey)) {
if (!this._storage.get(this.storageKey)) {
// This indicates we have moved to a new storage format so we migrate the value over
this.optInOut(otherStorage.get(this.storageKey) === '1');
}
otherStorage.remove(this.storageKey, this.config.cross_subdomain_cookie);
}
}
return this._storage;
},
enumerable: false,
configurable: true
});
ConsentManager.prototype.getDnt = function () {
if (!this.config.respect_dnt) {
return false;
}
return !!find([
navigator === null || navigator === void 0 ? void 0 : navigator.doNotTrack, // standard
navigator === null || navigator === void 0 ? void 0 : navigator['msDoNotTrack'],
assignableWindow['doNotTrack'],
], function (dntValue) {
return includes([true, 1, '1', 'yes'], dntValue);
});
};
return ConsentManager;
}());
export { ConsentManager };
//# sourceMappingURL=consent.js.map