// Copyright 2025 Circle Internet Group, Inc. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed 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.

//
//  PinSecretBridge.swift
//  CybavoWalletService
//
//  Created by Eva Hsu on 2019/10/22.
//

import Foundation
import CYBAVOWallet
@objc(PinSecretBridge)
class PinSecretBridge: NSObject{
    private static var pinSecrets = [Int : PinSecret]()
    public static let PIN_SECRET_KEY = "pinSecret"
    public static let PLAIN_TEXT = "plainText"
    public static let RETAIN = "retain"
    public static let REQUEST_ID = "requestId"
    public static let LEVEL = "level"
    public static let IS_SAME = "isSame"
    public static func get(key: Int) -> PinSecret?{

        return PinSecretBridge.pinSecrets[key]
    }

    public static func put(pinSecret: PinSecret) -> Int {
        let key = pinSecret.hashValue()/100000
        PinSecretBridge.pinSecrets[key] = pinSecret
        return key
    }

    public static func fromDictionary(dict: NSDictionary) -> PinSecret?{
        guard let key = dict[PinSecretBridge.PIN_SECRET_KEY] as! Int? else{
            return nil
        }
        let pinSecret1 = PinSecretBridge.pinSecrets[key]
        guard let pinSecret = pinSecret1 else{
            return nil
        }
        if let retain = dict[PinSecretBridge.RETAIN] as! Bool?, retain == true{
            pinSecret.retain()
        }else{
            PinSecretBridge.pinSecrets.removeValue(forKey: key)
        }
        return pinSecret
    }
}
