//
//  YHSelectCountryViewModel.swift
//  galaxy
//
//  Created by EDY on 2024/1/31.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHSelectCountryViewModel: NSObject {
    override init() {
        super.init()
    }
    
    func requestCountry(_ key: String, callBackBlock:@escaping (_ success: [YHCountryMessage?]?, _ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/country?keyword=\(key)"
        YHNetRequest.getRequest(url: strUrl) { [weak self] json, code in
            //1. json字符串 转 对象
            guard let self = self else { return }
            guard let model = NetBaseModel.deserialize(dict: json) else {
                let error : YHErrorModel = YHErrorModel(errorCode:YHErrorCode.dictParseError.rawValue,errorMsg: YHErrorCode.dictParseError.description())
                
                callBackBlock([],error)
                return
            }
////            printLog("model 是 ==> \(model)")
            let dic = model.data?.peel
            guard let resultArray = [YHCountryMessage].deserialize(array: dic as? [Any]) else {
                callBackBlock([],nil)
                return
            }
            
            callBackBlock(resultArray, nil)
        } failBlock: { err in
            callBackBlock([],err)
        }

    }
}

