const moment = require("moment");
const faqContent = require("./faq-content.js");

// links, videos, files and photos have different keys
// on the article and the static text object,
// so mapping the different keys here
// we could change the keys on the static text obj to
// match those on the article to remove the need for this.
const mapArticleKeyToStaticKey = {
  links: "link",
  videos: "video",
  files: "file",
  photos: "photo",
  audio: "audio",
  evaluation_links: "evaluation_links",
};

function staticLinkSetText(staticText, name, attr, type) {
  const mappedName = mapArticleKeyToStaticKey[name];
  let key;
  if (name === "links" && attr === "link") {
    key = `${mappedName}_${type}`;
  } else {
    key = `${mappedName}_${attr}_${type}`;
  }
  return staticText[key] || key;
}

function mapIdTitleToKeyValue(options) {
  if (!options) return null;
  return options.map(item => {
    return {
      key: item.id,
      value: item.title,
    };
  });
}

module.exports = {
  label: (staticText, name) => staticText[name + "_label"] || name + "_label",
  info: (staticText, name) => staticText[name + "_info"] || name + "_info",
  instructional: (staticText, name) => staticText[name + "_instructional"] || name + "_instructional",
  placeholder: (staticText, name) => staticText[name + "_placeholder"] || name + "_placeholder",
  staticText: (staticText, name) => staticText[name] || name,
  isEmptyArray: (article, name) => {
    const value = article[name];
    if (value && value.constructor === Array) {
      return value.length === 0;
    }
  },
  getvalue: (article, name) => {
    const item = article[name];
    if (item && item.hasOwnProperty("value")) {
      // if the item is an object with a value key, return that
      return item.value;
    } else {
      // otherwise just return the item
      return item;
    }
  },
  hasValue: (article, name) => {
    const item = article[name];

    // potential falsey values
    // null
    // ""
    // []
    // { "value": "" }

    return item !== null &&
           item !== "" &&
           !(item.hasOwnProperty("length") && item.length === 0) &&
           !(item.hasOwnProperty("value") && item.value === "");
  },
  getKey: (article, name) => {
    if (article[name]) {
      return  article[name].key;
    }
  },
  isSelectedInArray: (article, name, optionKey) => {
    const options = article[name];
    if (options && options.length > 0) {
      return options.find((item) => {
        return item.key === optionKey;
      });
    }
  },
  isSelected: (article, name, optionKey) => {
    const options = article[name];
    if (options) {
      return options.key === optionKey;
    }
  },
  getStaticOptions: (staticText, name) => {
    // has_components and is_component_of fields use the cases options
    // uses mapIdTitleToKeyValue function to map id/title keys to key/value keys
    if (name === "has_components" || name === "is_component_of") {
      return mapIdTitleToKeyValue(staticText["cases"]);
    } else if (name === "specific_methods_tools_techniques") {
      return mapIdTitleToKeyValue(staticText["methods"]);
    } else {
      return staticText[name];
    }
  },
  getOptions: (article, name) => {
    return article[name];
  },
  getLinkSetValue(article, name, index, attr) {
    if (!article[name]) return;
    if (!article[name][index]) return;
    return article[name][index][attr];
  },
  linkSetFieldName(name, index, attr) {
    return `${name}[${index}][${attr}]`;
  },
  linkSetPlaceholder(staticText, name, attr) {
    return staticLinkSetText(staticText, name, attr, "placeholder");
  },
  linkSetLabel(staticText, name, attr) {
    return staticLinkSetText(staticText, name, attr, "label");
  },
  linkSetInstructional(staticText, name, attr) {
    return staticLinkSetText(staticText, name, attr, "instructional");
  },
  formatDate(article, name, format) {
    return moment(article[name]).format(format);
  },
  getCaseEditSubmitType(req) {
    if (req.query.full === "1") {
      return "full";
    } else {
      return "quick";
    }
  },
  getFaqContent() {
    // todo: get this as translated text from the server
    return faqContent;
  },
};

@@ -969,16 +969,285 @@
  %7D);%0A%7D%0A%0A
+function getStaticTextValue(staticText, name, type) %7B%0A  const labelKey = %60$%7Bname%7D_$%7Btype%7D%60;%0A  const labelObj = staticText.labels.find(item =%3E item.key === labelKey);%0A%0A  if (labelObj && labelObj.value) %7B%0A    return labelObj.value;%0A  %7D else %7B%0A    return labelKey;%0A  %7D%0A%7D%0A%0A
 module.e
@@ -1280,33 +1280,36 @@
 cText, name) =%3E 
-s
+getS
 taticText%5Bname +
@@ -1305,44 +1305,40 @@
 Text
-%5Bname + %22_label%22%5D %7C%7C
+Value(staticText,
  name
- +
+,
  %22
-_
 label%22
+)
 ,%0A  
@@ -1357,33 +1357,36 @@
 cText, name) =%3E 
-s
+getS
 taticText%5Bname +
@@ -1382,42 +1382,39 @@
 Text
-%5Bname + %22_info%22%5D %7C%7C
+Value(staticText,
  name
- +
+,
  %22
-_
 info%22
+)
 ,%0A  
@@ -1442,33 +1442,36 @@
 cText, name) =%3E 
-s
+getS
 taticText%5Bname +
@@ -1467,46 +1467,33 @@
 Text
-%5Bname + %22_instructional%22%5D %7C%7C
+Value(staticText,
  name
- +
+,
  %22
-_
 inst
@@ -1502,16 +1502,17 @@
 ctional%22
+)
 ,%0A  plac
@@ -1534,33 +1534,36 @@
 cText, name) =%3E 
-s
+getS
 taticText%5Bname +
@@ -1559,44 +1559,33 @@
 Text
-%5Bname + %22_placeholder%22%5D %7C%7C
+Value(staticText,
  name
- +
+,
  %22
-_
 plac
@@ -1592,16 +1592,17 @@
 eholder%22
+)
 ,%0A  stat

write helper to accomodate new static text json structure