1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4"use strict";
5/* globals Localization */
6const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
7
8const L10N = new Localization([
9  "branding/brand.ftl",
10  "browser/branding/brandings.ftl",
11  "browser/branding/sync-brand.ftl",
12  "browser/newtab/onboarding.ftl",
13]);
14
15const ONBOARDING_MESSAGES = () => [
16  {
17    id: "FXA_ACCOUNTS_BADGE",
18    template: "toolbar_badge",
19    content: {
20      delay: 10000, // delay for 10 seconds
21      target: "fxa-toolbar-menu-button",
22    },
23    targeting: "false",
24    trigger: { id: "toolbarBadgeUpdate" },
25  },
26  {
27    id: "PROTECTIONS_PANEL_1",
28    template: "protections_panel",
29    content: {
30      title: { string_id: "cfr-protections-panel-header" },
31      body: { string_id: "cfr-protections-panel-body" },
32      link_text: { string_id: "cfr-protections-panel-link-text" },
33      cta_url: `${Services.urlFormatter.formatURLPref(
34        "app.support.baseURL"
35      )}etp-promotions?as=u&utm_source=inproduct`,
36      cta_type: "OPEN_URL",
37    },
38    trigger: { id: "protectionsPanelOpen" },
39  },
40];
41
42const OnboardingMessageProvider = {
43  async getExtraAttributes() {
44    const [header, button_label] = await L10N.formatMessages([
45      { id: "onboarding-welcome-header" },
46      { id: "onboarding-start-browsing-button-label" },
47    ]);
48    return { header: header.value, button_label: button_label.value };
49  },
50  async getMessages() {
51    const messages = await this.translateMessages(await ONBOARDING_MESSAGES());
52    return messages;
53  },
54  async getUntranslatedMessages() {
55    // This is helpful for jsonSchema testing - since we are localizing in the provider
56    const messages = await ONBOARDING_MESSAGES();
57    return messages;
58  },
59  async translateMessages(messages) {
60    let translatedMessages = [];
61    for (const msg of messages) {
62      let translatedMessage = { ...msg };
63
64      // If the message has no content, do not attempt to translate it
65      if (!translatedMessage.content) {
66        translatedMessages.push(translatedMessage);
67        continue;
68      }
69
70      // Translate any secondary buttons separately
71      if (msg.content.secondary_button) {
72        const [secondary_button_string] = await L10N.formatMessages([
73          { id: msg.content.secondary_button.label.string_id },
74        ]);
75        translatedMessage.content.secondary_button.label =
76          secondary_button_string.value;
77      }
78      if (msg.content.header) {
79        const [header_string] = await L10N.formatMessages([
80          { id: msg.content.header.string_id },
81        ]);
82        translatedMessage.content.header = header_string.value;
83      }
84      translatedMessages.push(translatedMessage);
85    }
86    return translatedMessages;
87  },
88};
89this.OnboardingMessageProvider = OnboardingMessageProvider;
90
91const EXPORTED_SYMBOLS = ["OnboardingMessageProvider"];
92