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

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

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

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