import { client } from '@operato/graphql';
import gql from 'graphql-tag';
import { showToast } from '../../shared/utils/alert';
export const getPowerPlants = async ({ filters = [], pagination = { page: 1, limit: 50 }, sortings = [] }) => {
    const response = await client.query({
        query: gql `
      query PowerPlants($filters: [Filter!], $pagination: Pagination, $sortings: [Sorting!]) {
        powerPlants(filters: $filters, pagination: $pagination, sortings: $sortings) {
          items {
            id
            name
            address
            weatherId
            capacity
            directCurrent
            alternatingCurrent
            generationAmount
            generationTime
            avgPrc
            maxInverterCommunicationStatus
            maxInverterOperationStatus
            isNoSignal
            usage
          }
          total {
            count
            capacity
            generationTime
            normalCount
            stopCount
            faultCount
            communicationFaultCount
            listCount
          }
        }
      }
    `,
        variables: {
            filters: filters,
            pagination: pagination,
            sortings: sortings
        }
    });
    if (response.errors) {
        showToast('요청에 실패하였습니다');
        return { status: false, data: { items: [], total: {} } };
    }
    return {
        status: true,
        data: {
            items: response.data.powerPlants.items,
            total: response.data.powerPlants.total
        }
    };
};
export const getPowerPlantDetail = async (id) => {
    const response = await client.query({
        query: gql `
      query PowerPlantDetail($id: String!) {
        powerPlantDetail(id: $id) {
          id
          alternatingCurrent
          todayGenerated
          name
          address
          capacity
          ceo
          ceoPhone
          weatherId
          rec
          safetyManager
          safetyManagerPhone
          collectedAt
        }
      }
    `,
        variables: { id }
    });
    if (response.errors) {
        showToast('요청에 실패하였습니다');
        return { status: false, data: {} };
    }
    return {
        status: true,
        data: response.data.powerPlantDetail
    };
};
export const getMyPowerPlant = async () => {
    const response = await client.query({
        query: gql `
      query MyPowerPlant {
        myPowerPlant {
          id
          refreshTime
        }
      }
    `
    });
    if (response.errors) {
        showToast('요청에 실패하였습니다');
        return { status: false, data: {} };
    }
    return {
        status: true,
        data: response.data.myPowerPlant
    };
};
// 대시 보드 화면 용 발전소 상세 정보 + 월간 합계
export const getPowerPlantDetailAndSummary = async (id) => {
    const response = await client.query({
        query: gql `
      query PowerPlantDetail($id: String!) {
        powerPlantDetail(id: $id) {
          id
          alternatingCurrent
          todayGenerated
          name
          address
          capacity
          ceo
          ceoPhone
          weatherId
          rec
          safetyManager
          safetyManagerPhone
          inverterManufacturer
          inverterModel
          collectedAt
        }

        powerPlantSummary(id: $id) {
          powerPlantId
          sum_acc
          avg_generation_time
        }
      }
    `,
        variables: { id }
    });
    if (response.errors) {
        showToast('요청에 실패하였습니다');
        return { status: false, data: { powerPlantDetail: {}, powerPlantSummary: {} } };
    }
    return {
        status: true,
        data: response.data
    };
};
export const getPowerPlantHeaderInfo = async (id) => {
    const response = await client.query({
        query: gql `
      query PowerPlantHeaderInfo($id: String!) {
        powerPlantHeaderInfo(id: $id) {
          id
          name
          capacity
          weatherId
          maxInverterOperationStatus
          maxInverterCommunicationStatus
          isNoSignal
        }
      }
    `,
        variables: { id }
    });
    if (response.errors) {
        showToast('요청에 실패하였습니다');
        return { status: false, data: {} };
    }
    return {
        status: true,
        data: response.data.powerPlantHeaderInfo
    };
};
export const getPowerPlantsForHeader = async (id) => {
    const response = await client.query({
        query: gql `
      query PowerPlantsForHeader($id: String!) {
        powerPlantsForHeader(id: $id) {
          id
          name
        }
      }
    `,
        variables: { id }
    });
    if (response.errors) {
        showToast('요청에 실패하였습니다');
        return { status: false, data: [] };
    }
    return {
        status: true,
        data: response.data.powerPlantsForHeader
    };
};
//# sourceMappingURL=query.js.map