"use strict";

/**
 * Licensed Materials - Property of IBM
 *
 * IBM Cognos Products: BI Glass
 *
 * Copyright IBM Corp. 2017
 *
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define(['underscore'], function (_) {
  'use strict'; //NOSONAR

  return {
    selectionChange: function selectionChange(selection, event) {
      switch (event.type) {
        case "set":
          {
            selection = {};
            selection[event.last] = true;
            break;
          }

        case "toggle":
          {
            if (selection[event.last]) {
              delete selection[event.last];
            } else {
              selection[event.last] = true;
            }

            break;
          }

        case "composite":
          {
            selection = {};

            for (var i = 0; i < event.added.length; ++i) {
              selection[event.added[i]] = true;
            }

            break;
          }

        default:
          {
            break;
          }
      }

      return selection;
    },
    createInitialState: function createInitialState(props) {
      var selection = {};

      _.each(props.parameter_value.values, function (savedValue) {
        _.find(props.spec.values, function (item, index) {
          if (item.value === savedValue.use) {
            selection[index] = true;
            return true;
          }

          return false;
        }.bind(this));
      }.bind(this));

      return selection;
    }
  };
});