// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/webui/settings/public/constants/routes_util.h"

#include <string_view>

#include "ash/constants/webui_url_constants.h"
#include "ash/webui/settings/public/constants/routes.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "url/gurl.h"

namespace chromeos::settings {

namespace {

std::string_view RemoveQuery(std::string_view path) {
  std::string_view::size_type input_index = path.find('?');
  if (input_index != std::string_view::npos) {
    return path.substr(0, input_index);
  }
  return path;
}

}  // namespace

bool IsOSSettingsSubPage(std::string_view sub_page) {
  // Sub-pages may have query parameters, e.g. networkDetail?guid=123456.
  std::string_view sub_page_without_query = RemoveQuery(sub_page);

  for (const char* p : kPaths) {
    std::string_view path_without_query = RemoveQuery(p);
    if (sub_page_without_query == path_without_query) {
      return true;
    }
  }

  return false;
}

GURL GetOSSettingsUrl(std::string_view sub_page) {
  DCHECK(sub_page.empty() || IsOSSettingsSubPage(sub_page)) << sub_page;
  return GURL(base::StrCat({ash::kChromeUIOSSettingsURL, sub_page}));
}

}  // namespace chromeos::settings
