{"version":3,"file":"main.DYsBBv.js","sources":["theme-6/virtual:theme","theme-6/discourse/initializers/init-discourse-reply-template-component.js","theme-6/virtual:entrypoint:main"],"sourcesContent":["import { getObjectForTheme } from \"discourse/lib/theme-settings-store\";\n\nexport const settings = getObjectForTheme(6);\n\nexport function themePrefix(key) {\n  return `theme_translations.6.${key}`;\n}\n","import { htmlSafe } from \"@ember/template\";\nimport { escape } from \"pretty-text/sanitizer\";\nimport { ajax } from \"discourse/lib/ajax\";\nimport { getOwnerWithFallback } from \"discourse/lib/get-owner\";\nimport { withPluginApi } from \"discourse/lib/plugin-api\";\nimport { emojiUnescape } from \"discourse/lib/text\";\nimport Composer from \"discourse/models/composer\";\nimport { i18n } from \"discourse-i18n\";\n\nfunction buildButton(dataset, extraClass) {\n  const action = dataset.action || \"reply\";\n  const label = dataset.label;\n\n  const button = document.createElement(\"button\");\n  button.classList.add(\"add-template\", \"btn\", \"btn-primary\");\n\n  if (extraClass) {\n    button.classList.add(extraClass);\n  }\n\n  button.innerText =\n    label ||\n    i18n(\n      themePrefix(`discourse_reply_template_component.use_template_${action}`)\n    );\n\n  return button;\n}\n\nfunction buildTagsList(tags) {\n  const tagsList = document.createElement(\"div\");\n  tagsList.classList.add(\"tags-list\");\n\n  tags.forEach((tag) => {\n    const checkbox = document.createElement(\"input\");\n    checkbox.classList.add(\"checkbox\");\n    checkbox.value = tag;\n    checkbox.type = \"checkbox\";\n\n    const checkboxDescription = document.createElement(\"span\");\n    checkboxDescription.classList.add(\"checkbox-description\");\n    checkboxDescription.innerHTML = htmlSafe(emojiUnescape(escape(tag)));\n\n    const checkboxWrapper = document.createElement(\"div\");\n    checkboxWrapper.classList.add(\"checkbox-wrapper\");\n    checkboxWrapper.appendChild(checkbox);\n    checkboxWrapper.appendChild(checkboxDescription);\n\n    tagsList.appendChild(checkboxWrapper);\n  });\n\n  return tagsList;\n}\n\nfunction localDateFormat(date) {\n  const options = {\n    date: date.format(\"YYYY-MM-DD\"),\n    timezone: moment.tz.guess(),\n  };\n\n  if (!date.startOf(\"day\").isSame(date) && !date.endOf(\"day\").isSame(date)) {\n    options.time = date.format(\"HH:mm\");\n  }\n\n  const parts = [];\n  Object.entries(options).forEach(([key, value]) => {\n    parts.push(`${key}=${value}`);\n  });\n\n  return `[${parts.join(\" \")}]`;\n}\n\nfunction _create(dataset, post, controllerOptions, title) {\n  return Object.assign(controllerOptions, {\n    action: Composer.CREATE_TOPIC,\n    title,\n  });\n}\n\nfunction _createPm(dataset, post, controllerOptions, title) {\n  return Object.assign(controllerOptions, {\n    action: Composer.PRIVATE_MESSAGE,\n    recipients: dataset.recipients,\n    usernames: dataset.usernames,\n    title,\n    archetypeId: \"private_message\",\n  });\n}\n\nfunction _reply(dataset, post, controllerOptions) {\n  return Object.assign(controllerOptions, {\n    action: Composer.REPLY,\n    topic: post.topic,\n  });\n}\n\nfunction _buildDraftKey(topicId, action) {\n  if (!action || action === \"reply\") {\n    return `topic_${topicId}`;\n  }\n\n  return \"new_topic\";\n}\n\nfunction openComposerWithTemplateAndAction(controller, post, wrap) {\n  const currentUser = getOwnerWithFallback(this).lookup(\"service:current-user\");\n  if (!currentUser) {\n    return;\n  }\n\n  const dataset = wrap.dataset;\n\n  return ajax(`/posts/${post.id}`, {\n    cache: false,\n  }).then((data) => {\n    const regex = new RegExp(\n      '\\\\[wrap=template.*?\\\\skey=\"?' +\n        dataset.key +\n        '\"?.*?\\\\]\\\\n((?:.|\\n)*?)\\\\n\\\\[\\\\/wrap\\\\]',\n      \"gm\"\n    );\n    const match = regex.exec(data.raw || \"\");\n\n    const formatContext = (context, date) =>\n      context === \"title\" ? date.format(\"L\") : localDateFormat(date);\n\n    const replacers = [\n      {\n        regex: /(\\$today)/g,\n        fn: (context) => {\n          const date = moment().startOf(\"day\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$tomorrow)/g,\n        fn: (context) => {\n          const date = moment().add(1, \"day\").startOf(\"day\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$week_start)/g,\n        fn: (context) => {\n          const date = moment().startOf(\"isoWeek\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$business_week_start)/g,\n        fn: (context) => {\n          const date = moment().day(\"monday\").startOf(\"day\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$week_end)/g,\n        fn: (context) => {\n          const date = moment().endOf(\"isoWeek\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$business_week_end)/g,\n        fn: (context) => {\n          const date = moment().day(\"friday\").endOf(\"day\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$prev_week_start)/g,\n        fn: (context) => {\n          const date = moment().subtract(1, \"week\").startOf(\"isoWeek\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$prev_business_week_start)/g,\n        fn: (context) => {\n          const date = moment()\n            .subtract(1, \"week\")\n            .day(\"monday\")\n            .startOf(\"day\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$prev_week_end)/g,\n        fn: (context) => {\n          const date = moment().subtract(1, \"week\").endOf(\"isoWeek\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$prev_business_week_end)/g,\n        fn: (context) => {\n          const date = moment().subtract(1, \"week\").day(\"friday\").endOf(\"day\");\n          return formatContext(context, date);\n        },\n      },\n      {\n        regex: /(\\$username)/g,\n        fn: () => {\n          return currentUser.username;\n        },\n      },\n      {\n        regex: /(\\$name)/g,\n        fn: () => {\n          if (currentUser?.name) {\n            return currentUser.name;\n          }\n          return \"\";\n        },\n      },\n      {\n        regex: /\\=([A-Z]*)\\=/g,\n        fn: (m) => {\n          // match any placeholder values set up via discourse-placeholder-theme-component\n          const strippedMatch = m.replace(/\\=/g, \"\");\n          const inputtedValue = document.querySelector(\n            `[data-wrap=\"placeholder\"] [data-key=\"${strippedMatch}\"]`\n          )?.value;\n          return inputtedValue || m;\n        },\n      },\n    ];\n\n    if (match && match[1]) {\n      replacers.forEach((replacer) => {\n        match[1] = match[1].replace(replacer.regex, replacer.fn);\n      });\n\n      let topicBody = match[1];\n      let title = dataset.title;\n\n      if (title) {\n        replacers.forEach((replacer) => {\n          title = title.replace(replacer.regex, () => replacer.fn(\"title\"));\n        });\n      }\n\n      const checkboxes = wrap.querySelectorAll(\"input[type=checkbox]:checked\");\n      if (checkboxes.length) {\n        const tags = Array.from(checkboxes, (cb) => cb.value);\n        topicBody += `\\n\\n${tags.join(\", \")}`;\n      }\n\n      let controllerOptions = {\n        topicBody,\n        draftKey:\n          controller.topicModel?.draft_key ||\n          _buildDraftKey(post.topic_id, dataset.action),\n        draftSequence: controller.topicModel?.draftSequence,\n        skipDraftCheck: true,\n        categoryId: dataset.categoryId || null,\n      };\n\n      switch (dataset.action) {\n        case \"create\":\n          controllerOptions = _create(dataset, post, controllerOptions, title);\n          break;\n        case \"create_pm\":\n          controllerOptions = _createPm(\n            dataset,\n            post,\n            controllerOptions,\n            title\n          );\n          break;\n        case \"reply\":\n        case null:\n        case undefined:\n          controllerOptions = _reply(dataset, post, controllerOptions);\n          break;\n      }\n\n      controller.open(controllerOptions);\n    }\n  });\n}\n\nexport default {\n  name: \"discourse-reply-template-component-setup\",\n\n  initialize(container) {\n    withPluginApi((api) => {\n      api.decorateCookedElement(\n        (cooked, helper) => {\n          const wraps = cooked.querySelectorAll(\n            'div.d-wrap[data-wrap=\"template\"]'\n          );\n\n          if (helper && wraps) {\n            const post = helper.getModel();\n\n            if (!post) {\n              return;\n            }\n\n            const controller =\n              getOwnerWithFallback(this).lookup(\"service:composer\");\n\n            wraps.forEach((wrap) => {\n              const key = wrap.dataset.key;\n              if (!key) {\n                const dialog = container.lookup(\"service:dialog\");\n                dialog.alert(\n                  i18n(\n                    themePrefix(\"discourse_reply_template_component.needs_key\")\n                  )\n                );\n              }\n\n              if ((wrap.innerText.match(/\\n/g) || []).length >= 20) {\n                const topButton = buildButton(wrap.dataset, \"top\");\n                topButton.addEventListener(\n                  \"click\",\n                  openComposerWithTemplateAndAction.bind(\n                    null,\n                    controller,\n                    post,\n                    wrap\n                  )\n                );\n                wrap.prepend(topButton);\n              }\n\n              if (wrap.dataset.tagsList && wrap.dataset.tagsList.length) {\n                const tags = wrap.dataset.tagsList.split(\",\").filter(Boolean);\n                wrap.appendChild(buildTagsList(tags));\n              }\n\n              const bottomButton = buildButton(wrap.dataset, \"bottom\");\n              bottomButton.addEventListener(\n                \"click\",\n                openComposerWithTemplateAndAction.bind(\n                  null,\n                  controller,\n                  post,\n                  wrap\n                )\n              );\n              wrap.appendChild(bottomButton);\n            });\n          }\n        },\n        { onlyStream: true, id: \"discourse-reply-template-component\" }\n      );\n    });\n  },\n};\n","import * as Mod1 from \"./discourse/initializers/init-discourse-reply-template-component\";\nconst compatModules = {\n  \"discourse/initializers/init-discourse-reply-template-component\": Mod1,\n};\nexport default compatModules;\n"],"names":["themePrefix","key","buildButton","dataset","extraClass","action","label","button","document","createElement","classList","add","innerText","i18n","openComposerWithTemplateAndAction","controller","post","wrap","currentUser","getOwnerWithFallback","this","lookup","ajax","id","cache","then","data","match","RegExp","exec","raw","formatContext","context","date","format","options","timezone","moment","tz","guess","startOf","isSame","endOf","time","parts","Object","entries","forEach","value","push","join","localDateFormat","replacers","regex","fn","day","subtract","username","name","m","strippedMatch","replace","inputtedValue","querySelector","replacer","topicBody","title","checkboxes","querySelectorAll","length","Array","from","cb","controllerOptions","draftKey","topicModel","draft_key","topicId","topic_id","draftSequence","skipDraftCheck","categoryId","assign","Composer","CREATE_TOPIC","_create","PRIVATE_MESSAGE","recipients","usernames","archetypeId","_createPm","undefined","REPLY","topic","_reply","open","getObjectForTheme","initDiscourseReplyTemplateComponent","initialize","container","withPluginApi","api","decorateCookedElement","cooked","helper","wraps","getModel","alert","topButton","addEventListener","bind","prepend","tagsList","tags","split","filter","Boolean","appendChild","tag","checkbox","type","checkboxDescription","innerHTML","htmlSafe","emojiUnescape","escape","checkboxWrapper","buildTagsList","bottomButton","onlyStream","compatModules"],"mappings":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