import { freezeSys, toPlainObject } from 'contentful-sdk-core';
import copy from 'fast-copy';
import { wrapCollection } from '../common-utils';
import enhanceWithMethods from '../enhance-with-methods';
import { wrapAgentRun } from './agent-run';
function createAgentApi(makeRequest) {
  const getParams = data => ({
    spaceId: data.sys.space.sys.id,
    environmentId: data.sys.environment.sys.id,
    agentId: data.sys.id
  });
  return {
    generate: function generate(payload) {
      const self = this;
      return makeRequest({
        entityType: 'Agent',
        action: 'generate',
        params: getParams(self),
        payload
      }).then(data => wrapAgentRun(makeRequest, data));
    }
  };
}
export function wrapAgent(makeRequest, data) {
  const agent = toPlainObject(copy(data));
  const agentWithMethods = enhanceWithMethods(agent, createAgentApi(makeRequest));
  return freezeSys(agentWithMethods);
}
export const wrapAgentCollection = wrapCollection(wrapAgent);