// 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.

#ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROTO_UTILS_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROTO_UTILS_H_

#include <optional>
#include <string>
#include <vector>

#include "chrome/browser/web_applications/proto/web_app.pb.h"
#include "chrome/browser/web_applications/proto/web_app_tab_strip.pb.h"
#include "chrome/browser/web_applications/proto/web_app_url_pattern.pb.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "components/services/app_service/public/cpp/icon_info.h"
#include "components/sync/protocol/web_app_specifics.pb.h"
#include "components/webapps/common/web_app_id.h"
#include "content/browser/background_fetch/background_fetch.pb.h"
#include "third_party/blink/public/common/manifest/manifest.h"
#include "third_party/blink/public/common/safe_url_pattern.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom-forward.h"
#include "third_party/protobuf/src/google/protobuf/repeated_ptr_field.h"

namespace apps {
struct IconInfo;
}

namespace web_app {

enum class RunOnOsLoginMode;

using RepeatedIconInfosProto =
    const ::google::protobuf::RepeatedPtrField<::sync_pb::WebAppIconInfo>;

using RepeatedImageResourceProto =
    const ::google::protobuf::RepeatedPtrField<content::proto::ImageResource>;

using RepeatedUrlPatternProto =
    ::google::protobuf::RepeatedPtrField<proto::UrlPattern>;

std::optional<apps::IconInfo::Purpose> SyncPurposeToIconInfoPurpose(
    sync_pb::WebAppIconInfo_Purpose purpose);

std::optional<std::vector<apps::IconInfo>> ParseAppIconInfos(
    const char* container_name_for_logging,
    const RepeatedIconInfosProto& manifest_icons_proto);

std::optional<std::vector<blink::Manifest::ImageResource>>
ParseAppImageResource(const char* container_name_for_logging,
                      const RepeatedImageResourceProto& manifest_icons_proto);

sync_pb::WebAppIconInfo_Purpose IconInfoPurposeToSyncPurpose(
    apps::IconInfo::Purpose purpose);

// Use the given |icon_info| to populate a |WebAppIconInfo| sync proto.
sync_pb::WebAppIconInfo AppIconInfoToSyncProto(const apps::IconInfo& icon_info);

// Use the given |image_resource| to populate a |ImageResource| proto.
content::proto::ImageResource AppImageResourceToProto(
    const blink::Manifest::ImageResource& image_resource);

RunOnOsLoginMode ToRunOnOsLoginMode(proto::WebApp::RunOnOsLoginMode mode);

proto::WebApp::RunOnOsLoginMode ToWebAppProtoRunOnOsLoginMode(
    RunOnOsLoginMode mode);

blink::mojom::DisplayMode ToMojomDisplayMode(
    proto::WebApp::DisplayMode display_mode);

proto::WebApp::DisplayMode ToWebAppProtoDisplayMode(
    blink::mojom::DisplayMode display_mode);

std::optional<blink::SafeUrlPattern> ToUrlPattern(
    const proto::UrlPattern& proto_url_pattern);

std::optional<std::vector<blink::SafeUrlPattern>> ToUrlPatterns(
    const RepeatedUrlPatternProto& proto_url_patterns);

proto::UrlPattern ToUrlPatternProto(const blink::SafeUrlPattern& url_pattern);

std::optional<TabStrip> ProtoToTabStrip(proto::TabStrip tab_strip_proto);

// Compute the relative manifest ID path as used in the sync proto.
std::string RelativeManifestIdPath(webapps::ManifestId manifest_id);

}  // namespace web_app

#endif  // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROTO_UTILS_H_
