// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const protobufjs = require('protobufjs');
const spec = require('./spec.json');

function main(channelConnection) {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  /**
   *  The channel_connection that the events are published from. For example:
   *  `projects/{partner_project_id}/locations/{location}/channelConnections/{channel_connection_id}`.
   */
  // const channelConnection = 'abc123'
  /**
   *  The CloudEvents v1.0 events to publish. No other types are allowed.
   */
  // const events = 1234

  // Imports the Publishing library
  const {PublisherClient} = require('@google-cloud/eventarc-publishing').v1;

  // Instantiates a client
  const publishingClient = new PublisherClient();

  async function callPublishChannelConnectionEvents() {
    const typeName = 'type.googleapis.com/io.cloudevents.v1.CloudEvent'.replace(
      /^.*\//,
      ''
    );
    //console.log(typeName);
    const root = protobufjs.Root.fromJSON(require('./spec.json'));
    const type = root.lookupType(typeName);
    const Any = root.lookupType('google.protobuf.Any');

    const event = type.fromObject({
      // '@type': 'type.googleapis.com/io.cloudevents.v1.CloudEvent',
      id: '12345',
      source: '//weatherco/example',
      specVersion: '1.0',
      //type: 'weatherco.v1.forecast',
      attributes: {
        time: {
          ceTimestamp: new Date('1970-01-01T00:00:01Z'),
        },
        datacontenttype: {
          ceString: 'application/json',
        },
      },
      textData: '{"message": "test message 123"}',
    });
    console.log(event);

    const eventy = {
      '@type': 'type.googleapis.com/io.cloudevents.v1.CloudEvent',
      id: '12345',
      source: '//weatherco/example',
      specVersion: '1.0',
      type: 'weatherco.v1.forecast',
      attributes: {
        time: {
          ceTimestamp: '1970-01-01T00:00:01Z',
        },
        datacontenttype: {
          ceString: 'application/json',
        },
      },
      textData: '{"message": "test message 123"}',
    };
    const message = Any.fromObject({
      typeUrl: typeName,
      value: event,
    });

    console.log(type.encode(event).finish().toString());

    // const request = {
    //   channelConnection,
      // events: [any],
    // };

    // Run request
    const response = await publishingClient.publishChannelConnectionEvents(
      request
    );
    console.log(response);
  }

  callPublishChannelConnectionEvents();
}

process.on('unhandledRejection', err => {
  console.error(err.message);
  process.exitCode = 1;
});
main(...process.argv.slice(2));

@@ -2182,478 +2182,8 @@
 %7D);%0A
-    console.log(event);%0A%0A    const eventy = %7B%0A      '@type': 'type.googleapis.com/io.cloudevents.v1.CloudEvent',%0A      id: '12345',%0A      source: '//weatherco/example',%0A      specVersion: '1.0',%0A      type: 'weatherco.v1.forecast',%0A      attributes: %7B%0A        time: %7B%0A          ceTimestamp: '1970-01-01T00:00:01Z',%0A        %7D,%0A        datacontenttype: %7B%0A          ceString: 'application/json',%0A        %7D,%0A      %7D,%0A      textData: '%7B%22message%22: %22test message 123%22%7D',%0A    %7D;
 %0A   
@@ -2277,69 +2277,8 @@
 %0A   
- console.log(type.encode(event).finish().toString());%0A%0A    //
  con
@@ -2295,19 +2295,16 @@
  = %7B%0A   
- //
    chann
@@ -2322,19 +2322,16 @@
 n,%0A     
- //
  events:
@@ -2336,20 +2336,21 @@
 s: %5B
-any
+message
 %5D,%0A   
- //
  %7D;%0A

fix for now