/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
export class AlarmStatusApiError extends Error {
    status;
    url;
    body;
    constructor(status, url, body) {
        super(`AlarmStatus ${status} ${url}${body ? ` — ${body.slice(0, 200)}` : ''}`);
        this.name = 'AlarmStatusApiError';
        this.status = status;
        this.url = url;
        this.body = body;
    }
}
export class AlarmStatusClient {
    fetchImpl;
    base;
    defaultHeaders;
    timeoutMs;
    constructor(options) {
        this.fetchImpl = options.fetch ?? globalThis.fetch.bind(globalThis);
        this.base = options.adminUrl.replace(/\/$/, '');
        this.defaultHeaders = options.headers ?? {};
        this.timeoutMs = options.timeoutMs ?? 0;
    }
    /** `GET /status/alarm/rules` — every loaded rule, per OAP node. */
    async listRules() {
        return this.json('/status/alarm/rules');
    }
    /** `GET /status/alarm/{ruleId}` — full body for one rule, per OAP
     *  node. */
    async ruleDetail(ruleId) {
        return this.json(`/status/alarm/${encodeURIComponent(ruleId)}`);
    }
    /** `GET /status/alarm/{ruleId}/{entityName}` — running window state
     *  for one entity. */
    async ruleContext(ruleId, entityName) {
        return this.json(`/status/alarm/${encodeURIComponent(ruleId)}/${encodeURIComponent(entityName)}`);
    }
    async json(path) {
        const url = `${this.base}${path}`;
        const init = {
            method: 'GET',
            headers: { Accept: 'application/json', ...this.defaultHeaders },
        };
        let timer = null;
        if (this.timeoutMs > 0) {
            const ctrl = new AbortController();
            timer = setTimeout(() => ctrl.abort(), this.timeoutMs);
            init.signal = ctrl.signal;
        }
        try {
            const res = await this.fetchImpl(url, init);
            if (!res.ok) {
                const body = await res.text();
                throw new AlarmStatusApiError(res.status, url, body);
            }
            return (await res.json());
        }
        finally {
            if (timer)
                clearTimeout(timer);
        }
    }
}
//# sourceMappingURL=alarm-status.js.map