'use strict';

var getQuery = function getQuery(type) {
  var conditions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  var articleCount = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;

  //Added feature to include additional conditions and parameters for site level operations
  //Site level conditions should start with && (logical AND), and site level params should end with , (comma) in order to execute query

  switch (type) {
    case 'related':
      return '*[\n                _type == "article"\n                && defined(title)\n                && defined(url)\n                && !(_id in path("drafts.**"))\n                && is_visible == true\n                && defined(published) \n                && dateTime(published) <= dateTime(now())\n                && passwordLock!=true\n                && taxonomyMapping[]._ref in $taxonomy\n                && taxonomyMapping[].disableAds != \'true\'\n                && contentCategory->.name != \'Poll\'\n                && contentCategory->.name != \'Slideshows\'\n                && contentCategory->.disableAds != \'true\'\n                && ExcludeFromInfiniteScroll !=true\n                && url.current != $url\n                && !defined(body[].videos)\n                ' + conditions + '\n              ]| order(published desc)[' + (articleCount ? articleCount : '$index') + ']{\n                  ' + params + '\n                  title,\n                  published,\n                  summary,\n                  thumbnail,\n                  ...,\n                  passwordLock,\n                  password,\n                  "authorDetails": authorMapping[]->{ displayName, url, profileImage, biography, showSocialShare },\n                  body[] {\n                    ...,\n                    asset->,\n                    images[]{\n                      ...,\n                      asset->\n                    },\n                    content[]{\n                      ...,\n                      asset->\n                    },\n                    videos[]{\n                      ...,\n                      thumbnail{\n                        _type,\n                        asset->\n                      }\n                    },\n                    response[]{\n                      ...,\n                      asset->\n                    },\n                    quizzes[]{\n                      ...,\n                      answer[]{\n                        ...,\n                        asset->\n                      },\n                      question[]{\n                        ...,\n                        asset->\n                      }\n                    },\n                    poster {\n                      _type,\n                      asset->\n                    },\n                    slides[]{\n                      ...,\n                      slideshowImage{\n                      ...,\n                      asset->\n                      }\n                    },\n                    upload_doc {\n                      _type,\n                      asset->\n                    },\n                    uploadAudio {\n                      _type,\n                      asset->\n                    }\n                  },\n                  \'category\':contentCategory->{\n                    name,\n                    disableAds\n                  },\n                  \'content_placement\': taxonomyMapping[]-> {\n                    _id,\n                    \'ancestor\': parent->parent->identifier,\n                    \'parent\': parent->identifier,\n                    \'name\': name,\n                    \'path\': identifier,\n                    disableAds,\n                    showSocialShare\n                  },\n                  documentGroup-> {\n                    name,\n                    thumbnail,\n                    parent->{...,parent->},\n                    isShow,\n                    showSocialShare,\n                    \'path\': identifier.current,\n                    adTargetingURL\n                  },\n                  documentGroupMapping[]-> {\n                    _id,\n                    name,\n                    thumbnail,\n                    parent->{...,parent->},\n                    isShow,\n                    showSocialShare,\n                    \'path\': identifier.current,\n                    adTargetingURL\n                  },\n                  \'issue\' : *[_type == \'publication\' && !(_id in path("drafts.**")) && is_visible == true && _id == ^.issueGroup._ref][0] {\n                    name,\n                    \'path\': identifier.current,\n                    \'publication\' : *[_type == \'publication\' && !(_id in path("drafts.**")) && is_visible == true && _id == ^.parent._ref][0] {\n                      name,\n                      \'path\': identifier.current\n                    },\n                    \'url\' : pdf.asset-> url\n                  }\n                }';
    case 'article':
      return '*[\n                        _type == "article"\n                        && !(_id in path("drafts.**"))\n                        && defined(title)\n                        && defined(url)\n                        && is_visible != false\n                        && url.current == $url\n                        ' + conditions + '\n                      ][0]{\n                          ' + params + '\n                          title,\n                          published,\n                          summary,\n                          thumbnail,\n                          ...,\n                          passwordLock,\n                          password,\n                          issueGroup-> { _id, name, identifier, parent-> },\n                          "authorDetails": authorMapping[]->{ displayName, url, profileImage, biography, showSocialShare },\n                          body[] {\n                            ...,\n                            asset->,\n                            images[]{\n                              ...,\n                              asset->\n                            },\n                            content[]{\n                              ...,\n                              asset->\n                            },\n                            videos[]{\n                              ...,\n                              thumbnail{\n                                _type,\n                                asset->\n                              }\n                            },\n                            response[]{\n                              ...,\n                              asset->\n                            },\n                            quizzes[]{\n                              ...,\n                              answer[]{\n                                ...,\n                                asset->\n                              },\n                              question[]{\n                                ...,\n                                asset->\n                              }\n                            },\n                            poster {\n                              _type,\n                              asset->\n                            },\n                            slides[]{\n                              ...,\n                              slideshowImage{\n                              ...,\n                              asset->\n                              }\n                            },\n                            upload_doc {\n                              _type,\n                              asset->\n                            },\n                            uploadAudio {\n                              _type,\n                              asset->\n                            },\n                            medias[]{\n                              ...,\n                              upload_doc {\n                                _type,\n                                asset-> \n                              },\n                              poster {\n                                _type,\n                                asset-> \n                              }\n                            },\n                            articles[]{\n                              ...,\n                              thumbnail{\n                                ...,\n                                asset-> \n                              },\n                              "authorDetails": authorMapping[]->{ displayName, url, profileImage, biography, showSocialShare },\n                              text[]{\n                                ...,\n                                upload_doc {\n                                  _type,\n                                  asset->\n                                },\n                                uploadAudio {\n                                  _type,\n                                  asset->\n                                },\n                              }\n                            }\n                          },\n                          \'content_placement\': taxonomyMapping[]-> {\n                            ...,\n                            \'ancestor\': parent->parent->identifier,\n                            \'parent\': parent->identifier,\n                            \'name\': name,\n                            \'path\': identifier,\n                            showSocialShare\n                          },\n                          documentGroup-> {\n                            _id,\n                            name,\n                            thumbnail,\n                            parent->{...,parent->},\n                            isShow,\n                            isSeries,\n                            showSocialShare,\n                            \'path\': identifier.current,\n                            adTargetingURL\n                          },\n                          documentGroupMapping[]-> {\n                            _id,\n                            name,\n                            thumbnail,\n                            parent->{...,parent->},\n                            isShow,\n                            showSocialShare,\n                            \'path\': identifier.current\n                          },\n                          \'category\':contentCategory->{\n                            name,\n                            disableAds\n                          },\n                          \'issue\' : *[_type == \'publication\' && !(_id in path("drafts.**")) && is_visible == true && _id == ^.issueGroup._ref][0] {\n                            name,\n                            volume,\n                            \'issueNumber\': number,\n                            \'articleSource\': source,\n                            \'path\': identifier.current,\n                            \'publication\' : *[_type == \'publication\' && !(_id in path("drafts.**")) && is_visible == true && _id == ^.parent._ref][0] {\n                              name,\n                              \'path\': identifier.current\n                            },\n                            \'url\' : pdf.asset-> url\n                          }\n\n                        }';
    case 'publication':
      return '*[_type == \'publication\'\n        && identifier.current == $publication\n        ' + conditions + '][0] {\n          ' + params + '\n          ...,\n          \'issues\': *[_type == \'publication\'\n              && is_visible\n              && references(^._id)\n              && defined(identifier)] {\n                ...,\n                thumbnail{ ..., asset-> },\n                taxonomyMapping[]->,\n                pdf{asset->},\n              } | order(year desc, month desc)\n      }';
    case 'issue':
      return '*[_type == \'publication\'\n        && identifier.current == $publication\n        ' + conditions + '][0] {\n            ' + params + '\n            ...,\n            \'issue\': *[_type == \'publication\'\n                && references(^._id)\n                && identifier.current == $issue][0] {\n                    ...,\n                    \'articles\': *[_type == \'article\'\n                        && !(_id in path("drafts.**"))\n                        && defined(title)\n                        && is_visible\n                        && defined(url)\n                        && defined(published) \n                        && dateTime(published) <= dateTime(now())\n                        && passwordLock != true\n                        && references(^._id)] | order(published desc) {\n                          ...,\n                          thumbnail{ ..., asset-> },\n                          authorMapping[0]->,\n                          contentCategory->,\n                          issueSection-> {\n                            name\n                          },\n                          \'pageNumber\': 1\n                        }\n                }\n        }';
    case 'externalResources':
      return '{\n        \'archived\': *[_type == "external" && archived && isVisible ' + conditions + '] | order(displayDate desc){ ..., thumbnail{ ..., asset-> }},\n        \'recent\': *[_type == "external" && archived != true && isVisible ' + conditions + '] | order(displayDate asc){ ..., thumbnail{ ..., asset-> }}\n      }';
    case 'mediaseries':
      return '\n        \n        ';
  }
};

module.exports = getQuery;
