#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

NS_INLINE NSBundle *{{ job.swiftIdentifier | objc_symbol_part }}__Bundle(void) {
{% if bundle.mode == "main" -%}
    return NSBundle.mainBundle;
{% elif bundle.mode == "module" -%}
#if defined(SWIFTPM_MODULE_BUNDLE)
    return SWIFTPM_MODULE_BUNDLE;
#else
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                   reason:@"SWIFTPM_MODULE_BUNDLE is unavailable for module-scoped ObjC resources."
                                 userInfo:nil];
#endif
{% elif bundle.mode == "custom" -%}
    NSBundle *bundle = [NSBundle bundleWithIdentifier:{{ bundle.identifier | objc_string_literal }}];
    if (bundle == nil) {
        @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                       reason:{{ ("Missing configured bundle identifier: " ~ bundle.identifier) | objc_string_literal }}
                                     userInfo:nil];
    }
    return bundle;
{% else -%}
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                   reason:{{ ("Unsupported bundle mode: " ~ bundle.mode) | objc_string_literal }}
                                 userInfo:nil];
{% endif -%}
}

{% macro render_entries(entries, prefix, table_name) -%}
{% for entry in entries -%}
{% if entry.kind == "namespace" -%}
{{ render_entries(entry.children, prefix ~ (entry.swiftIdentifier | objc_symbol_part) ~ "__", table_name) }}{% elif entry.kind == "string" -%}
NS_INLINE NSString *{{ job.swiftIdentifier | objc_symbol_part }}__{{ prefix }}{{ entry.swiftIdentifier | objc_symbol_part }}(void) {
    return NSLocalizedStringWithDefaultValue({{ entry.properties.key | objc_string_literal }}, {{ table_name | objc_string_literal }}, {{ job.swiftIdentifier | objc_symbol_part }}__Bundle(), @"", @"");
}

{% endif -%}
{% endfor -%}
{%- endmacro %}
{% for module in modules %}
{{ render_entries(module.entries, (module.name | objc_symbol_part) ~ "__", module.properties.tableName) }}{% endfor -%}
NS_ASSUME_NONNULL_END
