import {destination} from "../src";
import {JitsuDestinationContext} from "@jitsu/types/extension";
import {testDestination} from "jitsu-cli/lib/tests";
import {ElasticsearchDestinationConfig} from "../src/elasticsearch-destination";

/**
 * Represents context data of configured destination instance
 */
const testContext: JitsuDestinationContext<ElasticsearchDestinationConfig> = {
    destinationId: "abc123",
    destinationType: "elasticsearch",
    config: {
        anonymous: true,
        elasticsearch_domain: "http://elasticsearch.tangible.one",
        elasticsearch_port: "9200",
        elasticsearch_target: "jitsu",
        elasticsearch_apikey: "apikey"
    }
}

const date = new Date()
const isoDate = date.toISOString()
const epoch = date.getTime()

var posturl = testContext.config.elasticsearch_domain;
if(typeof testContext.config.elasticsearch_port !== "undefined" && testContext.config.elasticsearch_port !== ""){
  posturl = posturl + ":" + testContext.config.elasticsearch_port
}
posturl = posturl + "/" + testContext.config.elasticsearch_target + "/_doc/"

testDestination({
        name: "pageview",
        context: testContext,
        destination: destination,
        event: {
            event_type: "pageview",
            name: "pageview",
            domain: testContext.config.elasticsearch_domain,
            url: posturl,
        },
        expectedResult: [
            {
              "body": {
                  domain: "http://elasticsearch.tangible.one",
                  event_type: "pageview",
                  name: "pageview",
                  source_ip: "000.000.000.000",
                  url: posturl,
                },
                "headers": {
                    "Authorization": "ApiKey "+testContext.config.elasticsearch_apikey,
                    "User-Agent": undefined,
                    "Content-Type": "application/json",
                    "X-Forwarded-For": "000.000.000.000",
                },
                "method": "POST",
                "url": posturl
            }
        ]
    }
)
