#import "TQL10n.h"

@implementation TQL10n

+ (NSBundle *)resourceBundle {
    static NSBundle *bundle;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        // Framework 集成:资源在 framework bundle 内
        // SPM 集成:C/OC target 不支持 resources 参数,.strings 文件不在 bundle 中,
        //          localizedKey: 会 fallback 到 hardcoded 默认值。
        bundle = [NSBundle bundleForClass:[TQL10n class]];
    });
    return bundle;
}

+ (NSString *)localizedKey:(NSString *)key {
    // 1. SDK bundle（Framework 集成:.strings 在 framework 内）
    NSString *str = NSLocalizedStringFromTableInBundle(key, nil, [self resourceBundle], nil);
    if (![str isEqualToString:key]) return str;

    // 2. App main bundle（SPM 集成:客户将 .strings 加入 App 的 Copy Bundle Resources）
    str = NSLocalizedStringFromTableInBundle(key, nil, [NSBundle mainBundle], nil);
    if (![str isEqualToString:key]) return str;

    // 3. Hardcoded fallback（SPM 集成:客户未加 .strings）
    return [self fallbackForKey:key];
}

/// SPM 集成 fallback:与 Resources/zh-Hans.lproj/Localizable.strings 保持同步。
/// Framework 集成不会走到这里(bundle 中有 .strings 文件)。
+ (NSString *)fallbackForKey:(NSString *)key {
    if ([key isEqualToString:@"qian_default_title"])           return @"电子签署";
    if ([key isEqualToString:@"qian_confirm_close_title"])     return @"离开页面?";
    if ([key isEqualToString:@"qian_confirm_close_message"])   return @"当前流程尚未完成,离开后可能需要重新发起";
    if ([key isEqualToString:@"qian_confirm_leave"])           return @"离开";
    if ([key isEqualToString:@"qian_confirm_stay"])            return @"继续";
    if ([key isEqualToString:@"qian_js_ok"])                   return @"确定";
    if ([key isEqualToString:@"qian_js_cancel"])               return @"取消";
    if ([key isEqualToString:@"qian_compatibility_test_title"]) return @"电子签 H5 自检";
    return key;
}

+ (NSString *)defaultTitle           { return [self localizedKey:@"qian_default_title"]; }
+ (NSString *)confirmCloseTitle      { return [self localizedKey:@"qian_confirm_close_title"]; }
+ (NSString *)confirmCloseMessage    { return [self localizedKey:@"qian_confirm_close_message"]; }
+ (NSString *)confirmLeave           { return [self localizedKey:@"qian_confirm_leave"]; }
+ (NSString *)confirmStay            { return [self localizedKey:@"qian_confirm_stay"]; }
+ (NSString *)jsOK                   { return [self localizedKey:@"qian_js_ok"]; }
+ (NSString *)jsCancel               { return [self localizedKey:@"qian_js_cancel"]; }
+ (NSString *)compatibilityTestTitle { return [self localizedKey:@"qian_compatibility_test_title"]; }

@end
