<%
  const layers = locals.layers;
-%>
// DO NOT MODIFY
// THIS FILE IS AUTOGENERATED

#import "RCTMGLStyle.h"
#import "RCTMGLUtils.h"

@implementation RCTMGLStyle

- (id)initWithMGLStyle:(MGLStyle*)mglStyle
{
    if (self = [super init]) {
        _style = mglStyle;
    }
    return self;
}

<% for (const layer of layers) { %>
- (void)<%- setLayerMethodName(layer, 'ios') -%>:(<%- getLayerType(layer, 'ios') -%> *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
  if (![self _hasReactStyle:reactStyle]) {
    // TODO throw exception
    return;
  }

  NSArray<NSString*> *styleProps = [reactStyle allKeys];
  for (NSString *prop in styleProps) {
    if ([prop isEqualToString:@"__MAPBOX_STYLESHEET__"]) {
      continue;
    }

    RCTMGLStyleValue *styleValue = [RCTMGLStyleValue make:reactStyle[prop]];

  <% for (let i = 0; i < layer.properties.length; i++) { -%>
  <%- ifOrElseIf(i) -%> ([prop isEqualToString:@"<%= layer.properties[i].name %>"]) {
  <%_ if (layer.properties[i].image) { _%>
      if (![styleValue shouldAddImage]) {
        [self set<%- iosPropMethodName(layer, pascelCase(layer.properties[i].name)) -%>:layer withReactStyleValue:styleValue];
      } else {
        NSString *imageURI = [styleValue getImageURI];

        [RCTMGLUtils fetchImage:_bridge url:imageURI scale:[styleValue getImageScale] callback:^(NSError *error, UIImage *image) {
          if (image != nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
              if (isValid()) {
                [self->_style setImage:image forName:imageURI];
                [self set<%- iosPropMethodName(layer, pascelCase(layer.properties[i].name)) -%>:layer withReactStyleValue:styleValue];
              }
            });
          }
        }];
      }
  <%_ } else { _%>
      [self set<%- iosPropMethodName(layer, pascelCase(layer.properties[i].name)) -%>:layer withReactStyleValue:styleValue];
  <%_ } _%>
  <%_ if (layer.properties[i].transition) { _%>
    } else if ([prop isEqualToString:@"<%= layer.properties[i].name %>Transition"]) {
      [self set<%- iosPropMethodName(layer, pascelCase(layer.properties[i].name)) -%>Transition:layer withReactStyleValue:styleValue];
  <%_ } _%>
  <% } -%>
  } else {
      // TODO throw exception
    }
  }
}
<% } %>

<% for (const layer of layers) {%>
<% for (const prop of layer.properties) {%>
- (void)set<%- iosPropMethodName(layer, pascelCase(prop.name)) -%>:(<%- getLayerType(layer, 'ios') -%> *)layer withReactStyleValue:(RCTMGLStyleValue *)styleValue
{
    <%_ if (layer.name === 'light' && prop.name === 'position') { _%>
    layer.position = [styleValue getSphericalPosition];
    <%_ } else if (prop.name === 'visibility') { _%>
    layer.visible = [styleValue isVisible];
    <%_ } else { _%>
    layer.<%- iosPropName(prop.name) -%> = styleValue.mglStyleValue;
    <%_ } _%>
}
<%_ if (prop.transition) { _%>

- (void)set<%- iosPropMethodName(layer, pascelCase(prop.name)) -%>Transition:(<%- getLayerType(layer, 'ios') -%> *)layer withReactStyleValue:(RCTMGLStyleValue *)styleValue
{
    layer.<%- iosPropName(prop.name) -%>Transition = [styleValue getTransition];
}
<%_ } _%>
<% } %>
<% } %>

- (BOOL)_hasReactStyle:(NSDictionary *)reactStyle
{
  return reactStyle != nil && reactStyle.allKeys.count > 0;
}

@end
