// The copy these components render is the dashboard's: the en/it/es
// dictionaries stay the single source, reviewed and parity-tested where every
// other dashboard string is (ACC-362 §5, "reuse the dashboard dictionaries").
// The dashboard passes the slices below at runtime; the WordPress build copies
// the same slices into its bundle at build time (see vite.wp.config.ts), and
// nothing else from the dictionaries ships.
import type en from "../../../apps/dashboard/src/app/dictionaries/en.json";

type Dictionary = typeof en;

export type SiteAdminMessages = {
  widget: Dictionary["platform"]["domain"]["widget"];
  statement: Dictionary["platform"]["domain"]["statement"];
  /** The audit source labels ("Extension", "Dashboard", …) the picker names. */
  auditSource: Dictionary["platform"]["domain"]["audit"]["source"];
  /** An audit's outcome in the dashboard's words, as the statement's facts say it. */
  auditCompliance: Dictionary["platform"]["domain"]["audit"]["compliance"];
  /** The sales contact a customer from before the plans is offered. */
  sales: Dictionary["common"]["support"]["sales"];
  /**
   * The WordPress Tracker screen (ACC-412): its own words (`plugin`), and the
   * dashboard Tracker's status, history and run copy it shares, so a pause
   * or a verdict reads the same on both.
   */
  tracker: Pick<TrackerDictionary, "plugin" | "status" | "history" | "run">;
  /**
   * The WordPress Setup screen: connecting with an emailed code, and the
   * connection once made. WordPress only; the dashboard never renders it.
   */
  setup: Dictionary["platform"]["wordpressSetup"];
  /** Common words, and the ones the primitives say on their own (`labels`). */
  common: Pick<Dictionary["common"], "actions" | "errors" | "labels">;
};

type TrackerDictionary = Dictionary["platform"]["domain"]["tracker"];

/**
 * The words of the views notice's sentence when it offers view blocks by
 * card. Not a slice of `SiteAdminMessages`: only a host that sells them
 * passes these, with its purchase command (`SiteAdminViewsPurchase`), so the
 * WordPress bundle does not ship them.
 *
 * Declared by shape, not picked from the dictionary: the source the plugin
 * ships carries only the dictionary slices it renders, which have no
 * `platform.addOns`, and its type check would fail on the index. The
 * dashboard passes `platform.addOns.views`, which is checked against this
 * shape where it is passed.
 */
export type ViewsPurchaseWords = {
  planAboveFirst: string;
  planAboveFirstTerm: string;
};

/** The structural minimum of a dashboard dictionary this picks from. */
type DictionaryLike = {
  platform: {
    wordpressSetup: SiteAdminMessages["setup"];
    domain: {
      widget: SiteAdminMessages["widget"];
      statement: SiteAdminMessages["statement"];
      tracker: SiteAdminMessages["tracker"];
      audit: {
        source: SiteAdminMessages["auditSource"];
        compliance: SiteAdminMessages["auditCompliance"];
      };
    };
  };
  common: SiteAdminMessages["common"] & {
    support: { sales: SiteAdminMessages["sales"] };
  };
};

/**
 * The slices of a dashboard dictionary these components read. Shared by the
 * dashboard wrappers and the WordPress build so the two cannot pick different
 * keys.
 */
export function pickSiteAdminMessages(dict: DictionaryLike): SiteAdminMessages {
  return {
    widget: dict.platform.domain.widget,
    statement: dict.platform.domain.statement,
    tracker: {
      plugin: dict.platform.domain.tracker.plugin,
      status: dict.platform.domain.tracker.status,
      history: dict.platform.domain.tracker.history,
      run: dict.platform.domain.tracker.run,
    },
    setup: dict.platform.wordpressSetup,
    auditSource: dict.platform.domain.audit.source,
    auditCompliance: dict.platform.domain.audit.compliance,
    sales: dict.common.support.sales,
    common: {
      actions: dict.common.actions,
      errors: dict.common.errors,
      labels: dict.common.labels,
    },
  };
}

/**
 * A dashboard dictionary cut down to those slices, still in its own shape: what
 * the WordPress plugin ships as the source of its copy (export-source.ts), so
 * that `pickSiteAdminMessages` reads the same strings from it as from the whole.
 */
export function sliceSiteAdminDictionary(dict: DictionaryLike): DictionaryLike {
  const picked = pickSiteAdminMessages(dict);
  return {
    platform: {
      wordpressSetup: picked.setup,
      domain: {
        widget: picked.widget,
        statement: picked.statement,
        tracker: picked.tracker,
        audit: {
          source: picked.auditSource,
          compliance: picked.auditCompliance,
        },
      },
    },
    common: { ...picked.common, support: { sales: picked.sales } },
  };
}
