/*
 * @Author: Henry
 * @Date: 2026-04-07 19:23:07
 */
// packages/skill-shared/src/base-command.ts
import { Command } from "@oclif/core";
import { getAuthContext, forceRefreshAuthContext } from "./auth/index.js";
import { getConfig } from "./config.js";
import { APIClient } from "./http.js";
import { NotAuthenticatedError, MBSError, PermissionError } from "./errors.js";
export class MBSCommand extends Command {
    client;
    async init() {
        await super.init();
        const { cookie } = await getAuthContext();
        const { apiUrl } = getConfig();
        const refreshAuth = async () => {
            const { cookie: newCookie } = await forceRefreshAuthContext();
            return newCookie;
        };
        this.client = new APIClient(apiUrl, cookie, refreshAuth);
    }
    output(data, meta) {
        this.log(JSON.stringify({
            ok: true,
            data,
            ...(meta !== undefined ? { meta } : {}),
        }));
    }
    async catch(err) {
        if (err instanceof NotAuthenticatedError) {
            this.log(JSON.stringify({
                ok: false,
                error: { type: err.type, message: err.message, hint: err.hint },
            }));
            this.exit(2);
            return;
        }
        if (err instanceof PermissionError) {
            this.log(JSON.stringify({
                ok: false,
                error: { type: err.type, message: err.message, hint: err.hint },
            }));
            this.exit(1);
            return;
        }
        if (err instanceof MBSError) {
            this.log(JSON.stringify({
                ok: false,
                error: { type: err.type, message: err.message, hint: err.hint },
            }));
            this.exit(1);
            return;
        }
        this.log(JSON.stringify({
            ok: false,
            error: { type: "api", message: err.message, hint: "" },
        }));
        this.exit(1);
    }
}
//# sourceMappingURL=base-command.js.map