'use strict';

const log = require('./global-slimmer-registry-7bbdb1f2.js');
const changeManager = require('./change-manager-72f643de.js');
const staticSubjectTitle = require('./static-subject-title-5829a298.js');

/* eslint-disable @typescript-eslint/naming-convention */
const staticTranslations = {
  // Form validation rule
  'This field is required': 'This field is required',
  'The minimum input value is {0}': 'The minimum input value is {0}',
  'The maximum input value is {0}': 'The maximum input value is {0}',
  'The end value should be greater than the start value': 'The end value should be greater than the start value',
  'The end date should be later than start date': 'The end date should be later than start date',
  'Credit card number is invalid': 'Credit card number is invalid',
  'Please enter the correct email format.': 'Please enter the correct email format.',
  'The maximum input length is {0}': 'The maximum input length is {0}',
  'The start date should be later than {0}': 'The start date should be later than {0}',
  'The start date should be earlier than {0}': 'The start date should be earlier than {0}',
  'The end date should be earlier than {0}': 'The end date should be earlier than {0}',
  'The date should be later than {0}': 'The date should be later than {0}',
  'The date should be earlier than {0}': 'The date should be earlier than {0}',
  'The minimum input length is {0}': 'The minimum input length is {0}',
  'The end time should be later than start time': 'The end time should be later than start time',
  'Phone number is not valid': 'Phone number is not valid',
  'Phone number is valid but it belongs to country {0}, not country {1}': 'Phone number is valid but it belongs to country {0}, not country {1}',
  'The value does not exist in the source': 'The value does not exist in the source',
  'There are values that do not exist in the source': 'There are values that do not exist in the source',
  // Component internal text
  'Viewing': 'Viewing',
  'Show': 'Show',
  'Go To': 'Go To',
  'Viewing {0}-{1} of {2}': 'Viewing {0}-{1} of {2}',
  'Viewing [{0}-{1}] of {2}': 'Viewing [{0}-{1}] of {2}',
  'Show [{0} items / page]': 'Show [{0} items / page]',
  'Items Per Page': 'Items Per Page',
  'Upload': 'Upload',
  'Clear': 'Clear',
  'Today': 'Today',
  'Previous month': 'Previous month',
  'Next month': 'Next month',
  'Day': 'Day',
  'Week': 'Week',
  'Month': 'Month',
  'Sun': 'Sun',
  'Mon': 'Mon',
  'Tue': 'Tue',
  'Wed': 'Wed',
  'Thu': 'Thu',
  'Fri': 'Fri',
  'Sat': 'Sat',
  'Sun-short': 'S',
  'Mon-short': 'M',
  'Tue-short': 'T',
  'Wed-short': 'W',
  'Thu-short': 'T',
  'Fri-short': 'F',
  'Sat-short': 'S',
  'Sun-full': 'Sunday',
  'Mon-full': 'Monday',
  'Tue-full': 'Tuesday',
  'Wed-full': 'Wednesday',
  'Thu-full': 'Thursday',
  'Fri-full': 'Friday',
  'Sat-full': 'Saturday',
  'January': 'January',
  'February': 'February',
  'March': 'March',
  'April': 'April',
  'May': 'May',
  'June': 'June',
  'July': 'July',
  'August': 'August',
  'September': 'September',
  'October': 'October',
  'November': 'November',
  'December': 'December',
  'Jan': 'Jan',
  'Feb': 'Feb',
  'Mar': 'Mar',
  'Apr': 'Apr',
  'Jun': 'Jun',
  'Jul': 'Jul',
  'Aug': 'Aug',
  'Sep': 'Sep',
  'Oct': 'Oct',
  'Nov': 'Nov',
  'Dec': 'Dec',
  'All Day': 'All Day',
  'Select or drop your file here': 'Select or drop your file here',
  'Please sign here': 'Please sign here',
  'Clear Signature': 'Clear Signature',
  'Start Date': 'Start Date',
  'End Date': 'End Date',
  'Please select': 'Please select',
  'No options': 'No options',
  'No results found': 'No results found',
  'Minimum': 'Minimum',
  'Maximum': 'Maximum',
  'Select Time': 'Select Time',
  'Start Time': 'Start Time',
  'End Time': 'End Time',
  'There is no data to display': 'There is no data to display',
  'Text Styles': 'Text Styles',
  'Bold': 'Bold',
  'Italic': 'Italic',
  'Underline': 'Underline',
  'Strikethrough': 'Strikethrough',
  'Code': 'Code',
  'Text Color': 'Text Color',
  'Left Align': 'Left Align',
  'Center Align': 'Center Align',
  'Right Align': 'Right Align',
  'Justified': 'Justified',
  'Bullets': 'Bullets',
  'Numbers': 'Numbers',
  'More Indent': 'More Indent',
  'Less Indent': 'Less Indent',
  'Link': 'Link',
  'Image': 'Image',
  'Upload from computer': 'Upload from computer',
  'Insert via URL': 'Insert via URL',
  'Image via URL': 'Image via URL',
  'Insert': 'Insert',
  'Clear Style': 'Clear Style',
  'URL': 'URL',
  'Text': 'Text',
  'Edit': 'Edit',
  'Unlink': 'Unlink',
  'Size': 'Size',
  'Alt Text': 'Alt Text',
  'Delete Image': 'Delete Image',
  'Sm': 'Sm',
  'Md': 'Md',
  'Lg': 'Lg',
  'Title': 'Title',
  'Subtitle': 'Subtitle',
  'Heading 1': 'Heading 1',
  'Heading 2': 'Heading 2',
  'Heading 3': 'Heading 3',
  'Paragraph': 'Paragraph',
  'Reset Selection': 'Reset Selection',
  'Select Language': 'Select Language',
  'Delete Code': 'Delete Code',
  'Copy Code': 'Copy Code',
  'Code Block': 'Code Block',
  'Copied': 'Copied',
  'Repeat every {0} days': 'Repeat every {0} days',
  'Repeat daily': 'Repeat daily',
  'Repeat every {0} weeks on {1}': 'Repeat every {0} weeks on {1}',
  'Repeat weekly on {0}': 'Repeat weekly on {0}',
  'Repeat every {0} months on day {1}': 'Repeat every {0} months on day {1}',
  'Repeat monthly on day {0}': 'Repeat monthly on day {0}',
  'Repeat every {0} years on {1} {2}': 'Repeat every {0} years on {1} {2}',
  'Repeat yearly on {0} {1}': 'Repeat yearly on {0} {1}',
  'until {0}': 'until {0}',
  'after {0} occurrences': 'after {0} occurrences',
  'Sunday': 'Sunday',
  'Monday': 'Monday',
  'Tuesday': 'Tuesday',
  'Wednesday': 'Wednesday',
  'Thursday': 'Thursday',
  'Friday': 'Friday',
  'Saturday': 'Saturday',
  'am': 'am',
  'pm': 'pm',
  '{0} Selected': '{0} Selected',
  'date picker': 'Date Picker',
  'Selected result': 'Selected result',
  'Close event preview detail': 'Close event preview detail',
  'More actions': 'More actions',
  'Clear color': 'Clear color',
  'Color swatch': 'Color swatch',
  'Color {0}': 'Color {0}',
};

const en = /*#__PURE__*/Object.freeze({
  __proto__: null,
  staticTranslations: staticTranslations,
  'default': staticTranslations
});

/**
 * Vega translation controller to control the internal text translation
 */
class InternalTranslationController {
  constructor() {
    this.resources = { en: staticTranslations };
    this._currentLanguage = 'en';
  }
  /**
   * Get current language
   *
   * @returns {string} current language
   */
  get currentLanguage() {
    return this._currentLanguage;
  }
  /**
   * Initializes the translation controller with provided resources and language.
   *
   * @param {VegaTranslationConfig} config - The configuration object containing resources and initial language.
   */
  init({ resources, language }) {
    if (resources && Object.keys(resources).length > 0) {
      this.resources = resources;
    }
    else {
      log.LogUtility.warn(`Resources are empty. Falling back to default language "en".`);
      this.resources = { en: staticTranslations };
    }
    if (language && language in this.resources) {
      this._currentLanguage = language;
    }
    else {
      log.LogUtility.warn(`Language "${language}" not found in resources. Falling back to default language "en".`);
      this._currentLanguage = 'en';
    }
    changeManager.ChangeManager.notify(staticSubjectTitle.UPDATE_TRANSLATION, {});
  }
  /**
   * Changes the current language if it exists in the resources.
   *
   * @param {string} language - The language code to change to.
   */
  changeLanguage(language) {
    if (this.resources[language]) {
      this._currentLanguage = language;
      changeManager.ChangeManager.notify(staticSubjectTitle.UPDATE_TRANSLATION, {});
    }
    else {
      log.LogUtility.warn(`Language "${language}" not found in resources.`);
    }
  }
  /**
   * Retrieves the translated string for the given key based on the current language.
   *
   * @param {string} key - The key for the translation.
   * @param {unknown[]} args - A list of values to replace in the translation string.
   * @returns {string} The translated string or the key itself if not found.
   */
  t(key, ...args) {
    const translation = this.resources[this._currentLanguage][key] || staticTranslations[key] || key;
    return args.reduce((result, arg, index) => result.replace(new RegExp(`\\{${index}\\}`, 'g'), String(arg)), translation);
  }
  /**
   * Registers a component to the `connectedComponents` map, associating it with the component's tag name.
   * If the tag already exists, it adds the component to the existing set of components.
   *
   * @param {HTMLStencilElement} host - The component instance to register.
   */
  registerComponent(host) {
    const connectedComponents = InternalTranslationController.connectedComponents.get(host.tagName);
    if (connectedComponents) {
      connectedComponents.add(host);
    }
    else {
      InternalTranslationController.connectedComponents.set(host.tagName, new Set([host]));
    }
  }
  /**
   * Unregister a component from the `connectedComponents` map.
   *
   * @param {HTMLStencilElement} host - The component instance to unregister.
   */
  unRegisterComponent(host) {
    const connectedComponents = InternalTranslationController.connectedComponents.get(host.tagName);
    if (connectedComponents) {
      connectedComponents.delete(host);
    }
  }
  /**
   * Retrieves the map of connected components, where each entry associates a tag name with a set of components.
   *
   * @returns {Map<string, Set<HTMLElement>>} A map of connected components.
   */
  getConnectedComponents() {
    return InternalTranslationController.connectedComponents;
  }
  /**
   * Retrieves the preserved translated message segments.
   *
   * @param {string} key - The key for the translation.
   * @param {RegExp} preservePattern - the preserved regulation pattern to apply.
   * @param {unknown[]} args - A list of values to replace in the translation string.
   * @returns {PreservedTranslationOutput[]} An array of translated segments
   */
  tWithPreservedBlocks(key, preservePattern = /(\[[^\]]*\])/g, ...args) {
    // Use existing t method to handle translation and interpolation
    const interpolatedText = this.t(key, ...args);
    // Split by preserved blocks according to the pattern
    const segments = interpolatedText.split(preservePattern);
    // Convert to output format, marking preserved blocks
    return segments.map((segment) => {
      // Check if this segment matches the preserve pattern (by default: surrounded by square brackets)
      const isPreserved = preservePattern.test(segment);
      // For square brackets pattern, remove the brackets from content
      const content = isPreserved ? segment.substring(1, segment.length - 1) : segment;
      return {
        content,
        isPreserved,
      };
    });
  }
}
InternalTranslationController.connectedComponents = new Map();
const VegaInternalTranslation = new InternalTranslationController();

exports.VegaInternalTranslation = VegaInternalTranslation;
exports.en = en;
exports.staticTranslations = staticTranslations;
