var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import Keychain from 'react-native-keychain';
const USERNAME = '@CAPSULE';
export class KeychainStorage {
    get(key) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const item = yield Keychain.getGenericPassword({
                    service: key,
                });
                if (!item) {
                    return null;
                }
                return item.password;
            }
            catch (error) {
                console.warn('Error retrieving stored item:', error);
                return null;
            }
        });
    }
    set(key, value) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const result = yield Keychain.setGenericPassword(USERNAME, value, {
                    service: key,
                    accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED,
                    securityLevel: Keychain.SECURITY_LEVEL.ANY,
                    storage: Keychain.STORAGE_TYPE.AES_GCM_NO_AUTH,
                });
                if (!result) {
                    console.warn(`Failed to store key ${key}`);
                }
            }
            catch (error) {
                console.warn(`Error storing key ${key}:`, error);
            }
        });
    }
    removeItem(key) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield Keychain.resetGenericPassword({ service: key });
            }
            catch (error) {
                console.warn(`Error removing key ${key}:`, error);
            }
        });
    }
    clear(prefix) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const services = yield Keychain.getAllGenericPasswordServices();
                for (const key of services) {
                    if (key && key.startsWith(prefix)) {
                        try {
                            yield Keychain.resetGenericPassword({ service: key });
                        }
                        catch (error) {
                            console.warn(`Error clearing key ${key}:`, error);
                        }
                    }
                }
            }
            catch (error) {
                console.warn(`Error getting services for prefix ${prefix}:`, error);
            }
        });
    }
}
