import {isEmpty}from'lodash-es';const cityResolvers = () => ({
  City: {
    async state(src, arg, {
      stateDataLoader
    }) {
      // If it's already has been updated
      if (src?.state?.name) {
        return src.state;
      }
      return stateDataLoader.load(src.state.toString());
    }
  },
  Query: {
    async cities(_, args, {
      cityService,
      countryService,
      stateService
    }) {
      const {
        countryOptions,
        cityOptions,
        stateOptions
      } = args?.input || {};
      let countryIds;
      let stateIds;
      if (!isEmpty(countryOptions)) {
        const countriesPayload = await countryService.getAll({
          criteria: countryOptions
        });
        countryIds = countriesPayload?.map(item => item.id);
      }
      if (!isEmpty(stateOptions) || countryIds?.length) {
        const {
          criteria: statesCriteria,
          ...rest
        } = stateOptions || {};
        const statesPayload = await stateService.getAll({
          criteria: {
            ...statesCriteria,
            ...(countryIds?.length && {
              country: {
                $in: countryIds
              }
            }),
            ...rest
          }
        });
        stateIds = statesPayload?.map(({
          id
        }) => id);
      }
      const {
        criteria: citiesCriteria,
        ...restCitiesOptions
      } = cityOptions || {};
      const criteria = {
        ...citiesCriteria,
        ...{
          ...(stateIds?.length && {
            state: {
              $in: stateIds
            }
          })
        }
      };
      const updatedArgs = {
        ...restCitiesOptions,
        criteria
      };
      const data = await cityService.getAll({
        criteria
      });
      const totalCount = await cityService.count(updatedArgs);
      return {
        data,
        totalCount
      };
    },
    city(_, {
      id
    }, {
      cityService
    }, info) {
      return cityService.get(id);
    }
  },
  Mutation: {
    createCity(_, {
      propertyCity
    }, {
      cityService
    }) {
      return cityService.create(propertyCity);
    },
    updateCity(_, {
      id,
      propertyCity
    }, {
      cityService
    }) {
      return cityService.update(id, propertyCity, false);
    },
    deleteCity(_, {
      id
    }, {
      cityService
    }) {
      return cityService.delete(id);
    }
  }
});export{cityResolvers};//# sourceMappingURL=city.js.map
