/**
 * Returns an object containing the language messages and a function to generate
 * a required field message based on the provided selectLanguage parameter.
 *
 * @param {string} selectLanguage - The language code to use for the messages.
 * @return {Object} An object with two properties:
 *   - language: An object containing the language messages.
 *   - isRequired: A function that takes a field name and returns a required field
 *     message in the selected language.
 */
export default function useLanguage(selectLanguage = 'vi') {
  const data = import.meta.glob('@lang/*.json', {
    import: 'default',
    eager: true,
  })

  const imp = {}

  for (const path in data) {
    const keypath = path.match(/\/[a-zA-Z]+\./)[0].replace(/\/(\w+)\./, '$1')
    imp[keypath] = data[path]
  }

  /**
   * Returns a string representing a required field message in the selected language.
   *
   * @param {string} fieldName - The name of the field.
   * @return {string} The required field message.
   */
  const isRequired = (fieldName) =>
    imp[selectLanguage].message['IS_REQUIRED'].replace('%s', fieldName)

  return { language: imp[selectLanguage], isRequired }
}
