import gql from 'graphql-tag';
import { client } from '@operato/graphql';
export async function fetchProcessList({ filters, page, limit, sortings = [] }) {
    const response = await client.query({
        query: gql `
      query ($filters: [Filter!], $pagination: Pagination, $sortings: [Sorting!]) {
        processes: processesCreatedByMe(filters: $filters, pagination: $pagination, sortings: $sortings) {
          items {
            id
            name
            description
            group {
              id
              name
            }
            thumbnail
            createdAt
            updatedAt
          }
          total
        }
      }
    `,
        variables: {
            filters,
            pagination: { page, limit },
            sortings
        }
    });
    return response.data;
}
export async function fetchProcess(id) {
    const response = await client.query({
        query: gql `
      query FetchProcessById($id: String!) {
        process(id: $id) {
          id
          name
          description
          group {
            id
            name
          }
          thumbnail
          model
          createdAt
          creator {
            id
            name
          }
          updatedAt
          updater {
            id
            name
          }
        }
      }
    `,
        variables: { id }
    });
    return response.data;
}
export async function createProcess(process) {
    /*
      input NewProcess {
        name        : String!
        description : String
        model       : String!
        groupId     : String!
      }
    */
    process.model = JSON.stringify(process.model);
    const response = await client.mutate({
        mutation: gql `
      mutation CreateProcess($process: NewProcess!) {
        createProcess(process: $process) {
          id
          name
          description
          model
          createdAt
          updatedAt
        }
      }
    `,
        variables: {
            process
        }
    });
    return response.data;
}
export async function updateProcess(process) {
    /*
      input ProcessPatch {
        name        : String
        description : String
        model       : String
      }
      */
    var { id, name, description, model, groupId } = process;
    model = JSON.stringify(model);
    const response = await client.mutate({
        mutation: gql `
      mutation UpdateProcess($id: String!, $patch: ProcessPatch!) {
        updateProcess(id: $id, patch: $patch) {
          id
          name
          description
          model
          group {
            id
            name
          }
          createdAt
          updatedAt
        }
      }
    `,
        variables: {
            id,
            patch: { name, description, model, groupId }
        }
    });
    return response.data;
}
export async function deleteProcess(id) {
    const response = await client.mutate({
        mutation: gql `
      mutation ($id: String!) {
        deleteProcess(id: $id)
      }
    `,
        variables: {
            id
        }
    });
    return response.data;
}
export async function cloneProcess({ id, name, description, targetSubdomain, targetGroupId }) {
    const response = await client.mutate({
        mutation: gql `
      mutation CloneProcess($id: String!, $patch: ProcessPatch!, $targetSubdomain: String!, $targetGroupId: String) {
        cloneProcess(id: $id, patch: $patch, targetSubdomain: $targetSubdomain, targetGroupId: $targetGroupId) {
          id
          name
          description
          model
          group {
            id
            name
          }
          state
          version
          createdAt
          updatedAt
        }
      }
    `,
        variables: {
            id,
            patch: { name, description },
            targetSubdomain,
            targetGroupId
        }
    });
    return response.data;
}
export async function releaseProcess(process) {
    var { id } = process;
    const response = await client.mutate({
        mutation: gql `
      mutation ReleaseProcess($id: String!) {
        releaseProcess(id: $id) {
          id
          name
          description
          model
          group {
            id
            name
          }
          state
          version
          createdAt
          updatedAt
        }
      }
    `,
        variables: {
            id
        }
    });
    return response.data;
}
export async function revertProcessVersion(id, version) {
    const response = await client.mutate({
        mutation: gql `
      mutation RevertProcessVersion($id: String!, $version: Float!) {
        revertProcessVersion(id: $id, version: $version) {
          name
        }
      }
    `,
        variables: {
            id,
            version
        }
    });
    return response.data.revertProcessVersion;
}
//# sourceMappingURL=process.js.map