import { __esDecorate, __runInitializers } from "tslib";
import { observed, toRaw } from '../../decorators/observed.js';
/**
 * Represents a single rate limiting rule that applies to a specific
 * type of client, using a token bucket algorithm.
 */
let RateLimitRule = (() => {
    let _classSuper = EventTarget;
    let _description_decorators;
    let _description_initializers = [];
    let _description_extraInitializers = [];
    let _key_decorators;
    let _key_initializers = [];
    let _key_extraInitializers = [];
    let _rate_decorators;
    let _rate_initializers = [];
    let _rate_extraInitializers = [];
    let _interval_decorators;
    let _interval_initializers = [];
    let _interval_extraInitializers = [];
    let _burst_decorators;
    let _burst_initializers = [];
    let _burst_extraInitializers = [];
    return class RateLimitRule extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _description_decorators = [observed()];
            _key_decorators = [observed({ deep: true })];
            _rate_decorators = [observed()];
            _interval_decorators = [observed()];
            _burst_decorators = [observed()];
            __esDecorate(this, null, _description_decorators, { kind: "accessor", name: "description", static: false, private: false, access: { has: obj => "description" in obj, get: obj => obj.description, set: (obj, value) => { obj.description = value; } }, metadata: _metadata }, _description_initializers, _description_extraInitializers);
            __esDecorate(this, null, _key_decorators, { kind: "accessor", name: "key", static: false, private: false, access: { has: obj => "key" in obj, get: obj => obj.key, set: (obj, value) => { obj.key = value; } }, metadata: _metadata }, _key_initializers, _key_extraInitializers);
            __esDecorate(this, null, _rate_decorators, { kind: "accessor", name: "rate", static: false, private: false, access: { has: obj => "rate" in obj, get: obj => obj.rate, set: (obj, value) => { obj.rate = value; } }, metadata: _metadata }, _rate_initializers, _rate_extraInitializers);
            __esDecorate(this, null, _interval_decorators, { kind: "accessor", name: "interval", static: false, private: false, access: { has: obj => "interval" in obj, get: obj => obj.interval, set: (obj, value) => { obj.interval = value; } }, metadata: _metadata }, _interval_initializers, _interval_extraInitializers);
            __esDecorate(this, null, _burst_decorators, { kind: "accessor", name: "burst", static: false, private: false, access: { has: obj => "burst" in obj, get: obj => obj.burst, set: (obj, value) => { obj.burst = value; } }, metadata: _metadata }, _burst_initializers, _burst_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #description_accessor_storage = __runInitializers(this, _description_initializers, void 0);
        get description() { return this.#description_accessor_storage; }
        set description(value) { this.#description_accessor_storage = value; }
        #key_accessor_storage = (__runInitializers(this, _description_extraInitializers), __runInitializers(this, _key_initializers, void 0));
        get key() { return this.#key_accessor_storage; }
        set key(value) { this.#key_accessor_storage = value; }
        #rate_accessor_storage = (__runInitializers(this, _key_extraInitializers), __runInitializers(this, _rate_initializers, void 0));
        get rate() { return this.#rate_accessor_storage; }
        set rate(value) { this.#rate_accessor_storage = value; }
        #interval_accessor_storage = (__runInitializers(this, _rate_extraInitializers), __runInitializers(this, _interval_initializers, void 0));
        get interval() { return this.#interval_accessor_storage; }
        set interval(value) { this.#interval_accessor_storage = value; }
        #burst_accessor_storage = (__runInitializers(this, _interval_extraInitializers), __runInitializers(this, _burst_initializers, void 0));
        get burst() { return this.#burst_accessor_storage; }
        set burst(value) { this.#burst_accessor_storage = value; }
        constructor(state = {}) {
            super();
            __runInitializers(this, _burst_extraInitializers);
            this.description = state.description;
            this.key = RateLimitRule.isRateLimitRuleKey(state.key) ? structuredClone(state.key) : undefined;
            this.rate = typeof state.rate === 'number' ? state.rate : undefined;
            this.interval = RateLimitRule.isRateLimitRuleInterval(state.interval) ? state.interval : undefined;
            this.burst = typeof state.burst === 'number' ? state.burst : undefined;
        }
        toJSON() {
            const result = {};
            if (this.description) {
                result.description = this.description;
            }
            if (RateLimitRule.isRateLimitRuleKey(this.key)) {
                result.key = structuredClone(toRaw(this, this.key));
            }
            if (typeof this.rate === 'number') {
                result.rate = this.rate;
            }
            if (RateLimitRule.isRateLimitRuleInterval(this.interval)) {
                result.interval = this.interval;
            }
            if (typeof this.burst === 'number') {
                result.burst = this.burst;
            }
            return result;
        }
        notifyChange() {
            this.dispatchEvent(new Event('change'));
        }
        static isRateLimitRuleInterval(value) {
            if (typeof value !== 'string') {
                return false;
            }
            return ['second', 'minute', 'hour', 'day'].includes(value);
        }
        static isRateLimitRuleKey(value) {
            if (typeof value !== 'object' || value === null) {
                return false;
            }
            const key = value;
            return 'type' in key && (key.type === 'ip' || key.type === 'userId' || key.type === 'role');
        }
    };
})();
export { RateLimitRule };
//# sourceMappingURL=RateLimitRule.js.map