"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.systemCredentialStore = void 0;
exports.buildClaudeApiKeyCredentialRef = buildClaudeApiKeyCredentialRef;
exports.resolveClaudeApiKeyCredentialRef = resolveClaudeApiKeyCredentialRef;
exports.readStoredCredential = readStoredCredential;
exports.writeStoredCredential = writeStoredCredential;
exports.deleteStoredCredential = deleteStoredCredential;
const definitions_1 = require("./definitions");
function buildClaudeApiKeyCredentialRef(profileId) {
    return {
        credentialService: definitions_1.CLAUDE_API_KEY_CREDENTIAL_SERVICE,
        credentialAccount: `claude-api-key:${profileId}`,
    };
}
function resolveClaudeApiKeyCredentialRef(input) {
    if ("credentialService" in input) {
        return input;
    }
    return buildClaudeApiKeyCredentialRef(input.id);
}
function loadKeyringModule() {
    return require("@napi-rs/keyring");
}
exports.systemCredentialStore = {
    getPassword(ref) {
        const { Entry } = loadKeyringModule();
        return new Entry(ref.credentialService, ref.credentialAccount).getPassword();
    },
    setPassword(ref, password) {
        const { Entry } = loadKeyringModule();
        new Entry(ref.credentialService, ref.credentialAccount).setPassword(password);
    },
    deletePassword(ref) {
        const { Entry } = loadKeyringModule();
        return new Entry(ref.credentialService, ref.credentialAccount).deletePassword();
    },
};
function readStoredCredential(ref, store = exports.systemCredentialStore) {
    const value = store
        .getPassword(resolveClaudeApiKeyCredentialRef(ref))
        ?.trim();
    return value || null;
}
function writeStoredCredential(ref, password, store = exports.systemCredentialStore) {
    store.setPassword(resolveClaudeApiKeyCredentialRef(ref), password);
}
function deleteStoredCredential(ref, store = exports.systemCredentialStore) {
    store.deletePassword(resolveClaudeApiKeyCredentialRef(ref));
}
