<%- include('../partials/generatedHeader.ejs') -%>
import { connectionConfigVar } from "@prismatic-io/spectral/dist/integration";
import {
  findUserDefinedComponentKey,
  requireIntegrationContext,
} from "@prismatic-io/spectral/dist/serverTypes";
<%- include('../partials/imports.ejs', { imports, helpers }) %>

export interface <%= connection.typeInterface %>Values {
<%- include('../partials/performArgs.ejs', { inputs: connection.inputs, helpers }) -%>
}

/**
 * <%= connection.label %>
 *
 * @comments <%- connection.comments %>
 * This object is used to support type hinting.
 */
export const <%= connection.import %> = {
  key: "<%= connection.key %>",
  perform: (
    _values: <%= connection.typeInterface %>Values
  ): Promise<void> => Promise.resolve(),
  <%_ if (connection.onPremAvailable) { -%>
  onPremAvailable: true,
  <%_ } -%>
  inputs: {
    <%- include('../partials/inputs.ejs', { inputs: connection.inputs, helpers }) -%>
  }
} as const;

/**
 * <%= connection.label %> Connection Helper
 *
 * @comments This wrapper function can be used directly in config wizard definitions.
 */
export const <%= helpers.camelCase(connection.componentKey) %><%= helpers.capitalizeFirstLetter(helpers.camelCase(connection.key)) %> = (
  stableKey: string,
  values: {
<% connection.inputs.forEach((input) => { -%>
  <%- helpers.formatType(input.key) %><%= input.required ? "" : "?" %>: ({
    value: <%- input.valueType.type ? input.valueType.type : input.valueType %>;
  } | ConfigVarExpression) & ConfigVarVisibility & { writeOnly?: true };
<% }); -%>
  },
) => {
  return connectionConfigVar({
    dataType: "connection",
    stableKey,
    connection: {
      get component() {
        const context = requireIntegrationContext();
        return findUserDefinedComponentKey(
          "<%= connection.componentKey %>",
          <%= connection.componentIsPublic %>,
          context.componentRegistry,
        );
      },
      key: "<%= helpers.camelCase(connection.key) %>",
      values,
    },
  });
};
