/*
 * Copyright (C) 2017 - present Instructure, Inc.
 *
 * This module is part of Canvas.
 *
 * This module and Canvas are free software: you can redistribute them and/or modify them under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * This module and Canvas are distributed in the hope that they will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */
import formatMessage from '../format-message';
import _ from 'lodash';

const PILL_MAPPING = {
  'missing': { id: 'missing', text: formatMessage('Missing'), variant: 'danger' },
  'late': { id: 'late', text: formatMessage('Late'), variant: 'danger' },
  'graded': { id: 'graded', text: formatMessage('Graded') },
  'excused': { id: 'excused', text: formatMessage('Excused') },
  'submitted': { id: 'submitted', text: formatMessage('Submitted') },
  'new_grades': { id: 'new_grades', text: formatMessage('New Grades') },
  'new_feedback': { id: 'new_feedback', text: formatMessage('New Feedback') },
  'new_replies': { id: 'new_replies', text: formatMessage('New Replies') },
};

export function isNewActivityItem (item) {
  return item.newActivity;
}

export function anyNewActivity (items) {
  return items && items.some(isNewActivityItem);
}

/**
* Returns an array of pill objects that the particular item
* qualifies to have
*/
export function getBadgesForItem (item) {
  let badges = [];
  if (item.status) {
    badges = Object.keys(item.status)
      .filter(key => item.status[key] && PILL_MAPPING.hasOwnProperty(key))
      .map(a => PILL_MAPPING[a]);

    if (item.status.unread_count) {
      badges.push(PILL_MAPPING.new_replies);
    }
    if (item.newActivity && item.status.has_feedback) {
      badges.push(PILL_MAPPING.new_feedback);
    }
  }

  return badges;
}

/**
* Returns an array of pill objects that the items qualify to have
*/
export function getBadgesForItems (items) {
  const badges = [];
  if (items.some(i => i.status && i.newActivity && i.status.graded)) {
    badges.push(PILL_MAPPING.new_grades);
  }
  if (items.some(i => i.status && i.newActivity && i.status.has_feedback)) {
    badges.push(PILL_MAPPING.new_feedback);
  }
  if (items.some(i => i.status && i.status.unread_count)) {
    badges.push(PILL_MAPPING.new_replies);
  }
  return badges;
}

@@ -842,16 +842,23 @@
 ssing': 
+() =%3E (
 %7B id: 'm
@@ -913,24 +913,25 @@
 : 'danger' %7D
+)
 ,%0A  'late': 
@@ -930,16 +930,23 @@
 'late': 
+() =%3E (
 %7B id: 'l
@@ -999,16 +999,17 @@
 anger' %7D
+)
 ,%0A  'gra
@@ -1010,24 +1010,31 @@
   'graded': 
+() =%3E (
 %7B id: 'grade
@@ -1064,24 +1064,25 @@
 ('Graded') %7D
+)
 ,%0A  'excused
@@ -1080,24 +1080,31 @@
  'excused': 
+() =%3E (
 %7B id: 'excus
@@ -1136,24 +1136,25 @@
 'Excused') %7D
+)
 ,%0A  'submitt
@@ -1158,16 +1158,23 @@
 itted': 
+() =%3E (
 %7B id: 's
@@ -1214,24 +1214,25 @@
 ubmitted') %7D
+)
 ,%0A  'new_gra
@@ -1233,24 +1233,31 @@
 ew_grades': 
+() =%3E (
 %7B id: 'new_g
@@ -1295,24 +1295,25 @@
 w Grades') %7D
+)
 ,%0A  'new_fee
@@ -1320,16 +1320,23 @@
 dback': 
+() =%3E (
 %7B id: 'n
@@ -1386,16 +1386,17 @@
 back') %7D
+)
 ,%0A  'new
@@ -1406,16 +1406,23 @@
 plies': 
+() =%3E (
 %7B id: 'n
@@ -1470,16 +1470,17 @@
 lies') %7D
+)
 ,%0A%7D;%0A%0Aex
@@ -1956,16 +1956,18 @@
 PPING%5Ba%5D
+()
 );%0A%0A    
@@ -2036,24 +2036,26 @@
 .new_replies
+()
 );%0A    %7D%0A   
@@ -2146,24 +2146,26 @@
 new_feedback
+()
 );%0A    %7D%0A  %7D
@@ -2434,16 +2434,18 @@
 w_grades
+()
 );%0A  %7D%0A 
@@ -2561,16 +2561,18 @@
 feedback
+()
 );%0A  %7D%0A 
@@ -2626,24 +2626,24 @@
 d_count)) %7B%0A
-
     badges.p
@@ -2670,16 +2670,18 @@
 _replies
+()
 );%0A  %7D%0A 

fix pill localization