syntax = "proto3";

package devvit.ui.effects.web_view.v1alpha;

import "devvit/ui/effect_types/v1alpha/realtime_subscription.proto";
import "google/protobuf/struct.proto";

option go_package = "github.snooguts.net/reddit/reddit-devplatform-monorepo/go-common/generated/protos/types/devvit/ui/effects/web_view/v1alpha";
option java_package = "com.reddit.devvit.ui.effects.web_view.v1alpha";

enum WebViewInternalMessageScope {
  // Message is intended for the client only
  CLIENT = 0;
}

// Devvit internal message, sent via postMessage
message WebViewInternalMessage {
  // type: devvit-internal
  string type = 1;
  // Scope of the message, used to determine where the message should be sent
  WebViewInternalMessageScope scope = 2;

  oneof options {
    // Origin: devvit-analytics
    google.protobuf.Struct analytics = 3;
    // Origin: devvit-client
    google.protobuf.Struct effect = 4;
    // Origin: devvit-client
    devvit.ui.effect_types.v1alpha.RealtimeSubscriptionsEffect realtime_effect = 6;
  }

  optional string id = 5;
}

// Each message described below here will be converted to a Struct and sent to a WebView wrapped in a WebViewPostMessage:
//  Example, sending WebViewAppMessage:
//  {
//    type: 'devvit-message',
//    data: {
//      message: 'Foo'
//    }
//  }

// Structure of messages sent via postMessage
message WebViewPostMessage {
  // See comments for each message
  string type = 1;
  // One of the following messages converted to a Struct
  google.protobuf.Struct data = 2;
}

// Arbitrary message, sent by app via effect
// type: devvit-message
message WebViewAppMessage {
  // Any serializable value given to postMessage in a WebView
  // @deprecated Use json_string instead
  google.protobuf.Value message = 1;

  // JSON string payload
  optional string json_string = 2;
}

// Used to pass stateful data into a WebView
// type: devvit-state
// @deprecated
message WebViewStateMessage {
  google.protobuf.Struct state = 1;
}

// TODO: Add once UI messages are properly organized out of the block_kit package.
//       This is creating an import cycle in golang and cannot be compiled.
// Used to mirror some UIEvents to the WebView to handle it for apps automatically
// type: devvit-event
// message WebViewUIEventMessage {
//   devvit.ui.events.v1alpha.UIEvent event = 1;
// }

// TODO: Add once UI messages are properly organized out of the block_kit package.
//       This is creating an import cycle in golang and cannot be compiled.
// Used to inform a WebView of client environment changes
// type: devvit-environment
// message WebViewUIEnvironmentMessage {
//   devvit.ui.block_kit.v1beta.UIEnvironment environment = 1;
// }

message WebViewPostMessageEffect {
  // ID of the WebView block
  string web_view_id = 1;

  // Message to send to the WebView
  oneof message {
    WebViewAppMessage app = 2;
    WebViewStateMessage state = 3;
    // WebViewUIEventMessage event = 4;
    // WebViewUIEnvironmentMessage environment = 5;
  }
}
