import PluginSDK from "PluginSDK";

import { SERVER_ACTION } from "#SRC/js/constants/ActionTypes";
import AppDispatcher from "#SRC/js/events/AppDispatcher";
import GetSetBaseStore from "#SRC/js/stores/GetSetBaseStore";
import Util from "#SRC/js/utils/Util";

import {
  REQUEST_SDK_ENDPOINTS_SUCCESS,
  REQUEST_SDK_ENDPOINTS_ERROR,
  REQUEST_SDK_ENDPOINT_SUCCESS,
  REQUEST_SDK_ENDPOINT_ERROR
} from "../constants/ActionTypes";

import SDKEndpointActions from "../events/SDKEndpointActions";
import ServiceEndpoint from "../structs/ServiceEndpoint";

class SDKEndpointStore extends GetSetBaseStore {
  constructor() {
    super(...arguments);

    const self = this;

    this.getSet_data = {
      services: {}
    };

    PluginSDK.addStoreConfig({
      store: this,
      storeID: this.storeID,
      listenAlways: true
    });

    this.dispatcherIndex = AppDispatcher.register(function(payload) {
      if (payload.source !== SERVER_ACTION) {
        return false;
      }

      const { type, data } = payload.action;
      switch (type) {
        case REQUEST_SDK_ENDPOINTS_SUCCESS:
          self.processEndpoints(data.serviceId, data.endpoints);
          break;
        case REQUEST_SDK_ENDPOINTS_ERROR:
          self.setService(data.serviceId, {
            endpoints: [],
            error: data.error.description
          });
          break;
        case REQUEST_SDK_ENDPOINT_SUCCESS:
          self.processEndpoint(
            data.serviceId,
            data.endpointName,
            data.endpointData,
            data.contentType
          );
          break;
        case REQUEST_SDK_ENDPOINT_ERROR:
          self.setService(data.serviceId, {
            endpoints: [],
            error: data.error
          });
          break;
      }

      return true;
    });
  }

  getServices() {
    return Util.deepCopy(this.get("services"));
  }

  getServiceEndpoints(serviceId) {
    const service = this.getServices()[serviceId];

    if (!service || !service.endpoints) {
      return null;
    }

    return Object.entries(service.endpoints).map(
      ([endpointName, endpoint]) =>
        new ServiceEndpoint({
          endpointName,
          endpointData: endpoint.endpointData,
          contentType: endpoint.contentType
        })
    );
  }

  getServiceError(serviceId) {
    const service = this.getServices()[serviceId];

    if (!service || !service.error) {
      return "";
    }

    return service.error;
  }

  setService(serviceId, serviceData) {
    const services = this.getServices();
    services[serviceId] = {
      endpoints: serviceData.endpoints,
      error: serviceData.error
    };

    this.set({ services });
  }

  processEndpoints(serviceId, endpointsArray) {
    const endpoints = endpointsArray.reduce(function(acc, endpointName) {
      acc[endpointName] = {};

      return acc;
    }, {});

    this.setService(serviceId, {
      endpoints,
      error: ""
    });

    endpointsArray.forEach(function(endpoint) {
      SDKEndpointActions.fetchEndpoint(serviceId, endpoint);
    }, this);
  }

  processEndpoint(serviceId, endpointName, endpointData, contentType) {
    const service = this.getServices()[serviceId];
    if (!service || !service.endpoints[endpointName]) {
      return;
    }
    service.endpoints[endpointName] = { endpointData, contentType };

    this.setService(serviceId, {
      endpoints: service.endpoints,
      error: ""
    });
  }

  addChangeListener(eventName, callback) {
    this.on(eventName, callback);
  }

  removeChangeListener(eventName, callback) {
    this.removeListener(eventName, callback);
  }

  get storeID() {
    return "SDKEndpoint";
  }
}

module.exports = new SDKEndpointStore();

@@ -641,32 +641,8 @@
 );%0A%0A
-    const self = this;%0A%0A
     
@@ -658,24 +658,24 @@
 et_data = %7B%0A
+
       servic
@@ -856,25 +856,18 @@
 ter(
-function(
 payload
-)
+ =%3E
  %7B%0A 
@@ -1055,36 +1055,36 @@
 CESS:%0A          
-self
+this
 .processEndpoint
@@ -1179,36 +1179,36 @@
 RROR:%0A          
-self
+this
 .setService(data
@@ -1374,20 +1374,20 @@
         
-self
+this
 .process
@@ -1600,12 +1600,12 @@
     
-self
+this
 .set
@@ -3393,187 +3393,8 @@
  %7D%0A%0A
-  addChangeListener(eventName, callback) %7B%0A    this.on(eventName, callback);%0A  %7D%0A%0A  removeChangeListener(eventName, callback) %7B%0A    this.removeListener(eventName, callback);%0A  %7D%0A%0A
   ge

remove self and addChangeListener and removeChangeListener in SDKEndpoint Store