<commit_before>import AbstractInput from './abstract-input'
import * as listUtils from '../list-utils'
import utils from '../utils'
import Ember from 'ember'
import computed, {readOnly} from 'ember-computed-decorators'
import _ from 'lodash'

export default AbstractInput.extend({
  // ==========================================================================
  // Dependencies
  // ==========================================================================

  dbStore: Ember.inject.service('store'),

  // ==========================================================================
  // Properties
  // ==========================================================================

  classNames: [
    'frost-bunsen-input-select',
    'frost-field'
  ],

  getDefaultProps () {
    return {
      options: Ember.A([]),
      initialized: false
    }
  },

  selectedOptions: [],

  // ==========================================================================
  // Computed Properties
  // ==========================================================================

  @readOnly
  @computed('bunsenId', 'cellConfig', 'bunsenModel', 'bunsenStore.{disabled,formValue}')
  disabled (bunsenId, cellConfig, bunsenModel, formDisabled, value) {
    if (formDisabled || cellConfig.disabled || !bunsenModel) {
      return true
    }

    return !utils.hasValidQueryValues(value, bunsenModel.query, bunsenId)
  },

  // ==========================================================================
  // Functions
  // ==========================================================================

  didReceiveAttrs ({oldAttrs, newAttrs}) {
    this._super(...arguments)

    const modelDef = this.get('bunsenModel')
    if (!modelDef) {
      return
    }
    //on first run "this.get('initialized')" will always be 'false' & options will be set for component
    if(!this.get('initialized')){

       const dbStore = this.get('dbStore')
       const value = this.get('bunsenStore.formValue')
       const bunsenId = this.get('bunsenId')

       listUtils.getOptions(value, modelDef, bunsenId, dbStore).then((opts) => {
         this.set('options', opts)
       })
     }else if(this.get('initialized') && modelDef.query){
        if (this.hasQueryChanged(oldAttrs, newAttrs, modelDef.query)) {
           //setting required variables once above condition is true
           const dbStore = this.get('dbStore')
           const value = this.get('bunsenStore.formValue')
           const bunsenId = this.get('bunsenId')
           if(utils.hasValidQueryValues(value, modelDef.query, bunsenId)){
                 listUtils.getOptions(value, modelDef, bunsenId, dbStore).then((opts) => {
                   this.set('options', opts)
                 })
             }
        }
    }
  this.set('initialized', true)
  },

  /**
   * Checks if query has been changed
   * @param {Object} oldAttrs - old attributes
   * @param {Object} newAttrs - new attributes
   * @param {Object} modelQuery - query model
   * @returns {Boolean} true if query has been changed
   */
   hasQueryChanged (oldAttrs, newAttrs, modelQuery) {

     var modelQueryHasProperty = false;
     for(var prop in modelQuery){
        if(prop){
            modelQueryHasProperty = true;
        }
     }
      //If modelQuery has properties then evaluate further
      if(modelQueryHasProperty && this.get('initialized')){
        const bunsenId = this.get('bunsenId')
        const value = _.get(newAttrs, 'bunsenStore.value.formValue')
        const oldValue = _.get(oldAttrs, 'bunsenStore.value.formValue')

        const modelQuery_String = JSON.stringify(modelQuery)
        const valueVariable = modelQuery_String.split('${')[1].split('}')[0]

        //If valueVariable exists in newAttrs & oldAttrs only then evaluate further
        let value_result = utils.findValue(value, valueVariable, bunsenId)
        let oldValue_result = utils.findValue(oldValue, valueVariable, bunsenId)
          if(value_result && oldValue_result){

              let oldQuery
              let query
              //parse old and new query before look for differences
               try {
                  oldQuery = utils.populateQuery(oldValue, modelQuery, bunsenId)
               } catch (e) {
                  oldQuery = {}
               }

               try {
                 query = utils.populateQuery(value, modelQuery, bunsenId)
               } catch (e) {
                 query = {}
               }
               //returns false when every top level key/value pair are equal
               return !Object.keys(modelQuery).every((key) => {
                  return query[key] === oldQuery[key]
               })
        }
     }else{
        return false;
     }
},

  /**
   * Get variables for parsing template strings
   * @param {String} value - value of selected item
   * @returns {Object} variables<commit_msg>BPSO-24495<commit_after>import AbstractInput from './abstract-input'
import * as listUtils from '../list-utils'
import utils from '../utils'
import Ember from 'ember'
import computed, {readOnly} from 'ember-computed-decorators'
import _ from 'lodash'

export default AbstractInput.extend({
  // ==========================================================================
  // Dependencies
  // ==========================================================================

  dbStore: Ember.inject.service('store'),

  // ==========================================================================
  // Properties
  // ==========================================================================

  classNames: [
    'frost-bunsen-input-select',
    'frost-field'
  ],

  getDefaultProps () {
    return {
      options: Ember.A([]),
      initialized: false
    }
  },

  selectedOptions: [],

  // ==========================================================================
  // Computed Properties
  // ==========================================================================

  @readOnly
  @computed('bunsenId', 'cellConfig', 'bunsenModel', 'bunsenStore.{disabled,formValue}')
  disabled (bunsenId, cellConfig, bunsenModel, formDisabled, value) {
    if (formDisabled || cellConfig.disabled || !bunsenModel) {
      return true
    }

    return !utils.hasValidQueryValues(value, bunsenModel.query, bunsenId)
  },

  // ==========================================================================
  // Functions
  // ==========================================================================

  didReceiveAttrs ({oldAttrs, newAttrs}) {
    this._super(...arguments)

    const modelDef = this.get('bunsenModel')
    if (!modelDef) {
      return
    }
   if (this.hasQueryChanged(oldAttrs, newAttrs, modelDef.query)) {
           //setting required variables once above condition is true
           const dbStore = this.get('dbStore')
           const value = this.get('bunsenStore.formValue')
           const bunsenId = this.get('bunsenId')
           if(utils.hasValidQueryValues(value, modelDef.query, bunsenId)){
                 listUtils.getOptions(value, modelDef, bunsenId, dbStore).then((opts) => {
                   this.set('options', opts)
                 })
             }
    }

  this.set('initialized', true)
  },

  /**
   * Checks if query has been changed
   * @param {Object} oldAttrs - old attributes
   * @param {Object} newAttrs - new attributes
   * @param {Object} modelQuery - query model
   * @returns {Boolean} true if query has been changed
   */
   hasQueryChanged (oldAttrs, newAttrs, modelQuery) {
     // allow models that don't have query defined to pass as well as
     // allow the options to get initially populated
     if (!modelQuery || !this.get('initialized')) {
       return true
     }
     
     var modelQueryHasProperty = false;
     for(var prop in modelQuery){
        if(prop){
            modelQueryHasProperty = true;
        }
     }
      //If modelQuery has properties then evaluate further
      if(modelQueryHasProperty && this.get('initialized')){
        const bunsenId = this.get('bunsenId')
        const value = _.get(newAttrs, 'bunsenStore.value.formValue')
        const oldValue = _.get(oldAttrs, 'bunsenStore.value.formValue')

        const modelQuery_String = JSON.stringify(modelQuery)
        const valueVariable = modelQuery_String.split('${')[1].split('}')[0]

        //If valueVariable exists in newAttrs & oldAttrs only then evaluate further
        let value_result = utils.findValue(value, valueVariable, bunsenId)
        let oldValue_result = utils.findValue(oldValue, valueVariable, bunsenId)
          if(value_result || oldValue_result){

              let oldQuery
              let query
              //parse old and new query before look for differences
               try {
                  oldQuery = utils.populateQuery(oldValue, modelQuery, bunsenId)
               } catch (e) {
                  oldQuery = {}
               }

               try {
                 query = utils.populateQuery(value, modelQuery, bunsenId)
               } catch (e) {
                 query = {}
               }
               //returns false when every top level key/value pair are equal
               return !Object.keys(modelQuery).every((key) => {
                  return query[key] === oldQuery[key]
               })
        }
     }else{
        return false;
     }
},

  /**
   * Get variables for parsing template strings
   * @param {String} value - value of selected item
   * @returns {Object} variables
{'fraction_non_alphanumeric': 0.22591888466413182, 'fraction_numerical': 0.0009505703422053232, 'mean_word_length': 2.7017200938232993, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7566476', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2419, 'n_words': 580}