<commit_before>'use strict';

var DataStore = require('./../data-store/data-store');
var Logger = require('./../logger/logger');
var Utilities = require('./../utilities/utilities');
var VirtualDOM = require('./../virtual-dom/virtual-dom');

var FUNC = 'function';
var REPEAT_INDEX_KEY = '$index';
var REPEAT_PAYLOAD_KEY = '$repeatPayload';


function Behaviors(behaviorGroups) {
    this.behaviorList = Behaviors.processBehaviorGroups(behaviorGroups);
}

Behaviors.staticToDynamic = function staticToDynamic(value) {
    return function() {
        return value;
    };
};

Behaviors.prototype.getBehaviorList = function getBehaviorList() {
    return this.behaviorList;
};

Behaviors.prototype.eachListItem = function eachListItem(cb) {
    for (var i = 0; i < this.behaviorList.length; i++) {
        cb(this.behaviorList[i]);
    }
};

/**
 * Create flat list of behaviors & convert static behaviors to dynamic
 * in order to have a standardized list for easy processing.
 *
 * @param  {Object} behaviorGroups Behaviors grouped by CSS selector
 * @return {Array}                 Flat list of Behaviors
 */
Behaviors.processBehaviorGroups = function processBehaviorGroups(behaviorGroups) {
    var list = [];
    for (var behaviorSelector in behaviorGroups) {
        var behaviorImpls = behaviorGroups[behaviorSelector];
        for (var behaviorName in behaviorImpls) {
            var behaviorImpl = behaviorImpls[behaviorName];
            if (typeof behaviorImpl !== FUNC) {
                behaviorImpl = Behaviors.staticToDynamic(behaviorImpl);
            }
            var paramNames = Utilities.getParameterNames(behaviorImpl);
            list.push({
                name: behaviorName,
                selector: behaviorSelector,
                action: behaviorImpl,
                params: paramNames
            });
        }
    }
    return list;
};

Behaviors.getPayload = function getPayload(behavior, component) {
    if (typeof behavior.action === 'function') {
        var args = component.states.getValues(behavior.params);
        return behavior.action.apply(null, args);
    }
    else {
        return behavior.action;
    }
};

Behaviors.getPayloadFromUID = function getPayloadFromUID(behavior, uid) {
    return Behaviors.getPayload(behavior, DataStore.getComponent(uid));
};

/*
example:
famous:core:dom-element
    behaviors
        $self
            assign-content [searches own `$private` events, then `$public` events]
 */
Behaviors.selfMessagingBehavior = function selfMessagingBehavior(behavior, component) {
    var payload = Behaviors.getPayload(behavior, component);
    var event = component.events.getSelfEvent(behavior.name);
    component.events.triggerEvent(event, payload, component.uid);
};

/*
example:
famous:core:view
    behaviors
        $self
            famous:core:components:align
 */
Behaviors.selfDirectBehavior = function selfDirectBehavior(behavior, component) {
    var payload = Behaviors.getPayload(behavior, component);
    component.events.triggerDirectEventHandler(behavior.name, payload, component.uid);
};

/*
example:
famous:core:ui-element
    behaviors
        #view
            align
 */
Behaviors.targetMessagingBehavior = function targetMessagingBehavior(behavior, component, targets) {<commit_msg>Feat: Adds `cameraBehavior` to behaviors.js<commit_after>'use strict';

var DataStore = require('./../data-store/data-store');
var FamousConnector = require('./../famous-connector/famous-connector');
var Logger = require('./../logger/logger');
var Utilities = require('./../utilities/utilities');
var VirtualDOM = require('./../virtual-dom/virtual-dom');

var FUNC = 'function';
var REPEAT_INDEX_KEY = '$index';
var REPEAT_PAYLOAD_KEY = '$repeatPayload';


function Behaviors(behaviorGroups) {
    this.behaviorList = Behaviors.processBehaviorGroups(behaviorGroups);
}

Behaviors.staticToDynamic = function staticToDynamic(value) {
    return function() {
        return value;
    };
};

Behaviors.prototype.getBehaviorList = function getBehaviorList() {
    return this.behaviorList;
};

Behaviors.prototype.eachListItem = function eachListItem(cb) {
    for (var i = 0; i < this.behaviorList.length; i++) {
        cb(this.behaviorList[i]);
    }
};

/**
 * Create flat list of behaviors & convert static behaviors to dynamic
 * in order to have a standardized list for easy processing.
 *
 * @param  {Object} behaviorGroups Behaviors grouped by CSS selector
 * @return {Array}                 Flat list of Behaviors
 */
Behaviors.processBehaviorGroups = function processBehaviorGroups(behaviorGroups) {
    var list = [];
    for (var behaviorSelector in behaviorGroups) {
        var behaviorImpls = behaviorGroups[behaviorSelector];
        for (var behaviorName in behaviorImpls) {
            var behaviorImpl = behaviorImpls[behaviorName];
            if (typeof behaviorImpl !== FUNC) {
                behaviorImpl = Behaviors.staticToDynamic(behaviorImpl);
            }
            var paramNames = Utilities.getParameterNames(behaviorImpl);
            list.push({
                name: behaviorName,
                selector: behaviorSelector,
                action: behaviorImpl,
                params: paramNames
            });
        }
    }
    return list;
};

Behaviors.getPayload = function getPayload(behavior, component) {
    if (typeof behavior.action === 'function') {
        var args = component.states.getValues(behavior.params);
        return behavior.action.apply(null, args);
    }
    else {
        return behavior.action;
    }
};

Behaviors.getPayloadFromUID = function getPayloadFromUID(behavior, uid) {
    return Behaviors.getPayload(behavior, DataStore.getComponent(uid));
};

/*
example:
famous:core:dom-element
    behaviors
        $camera
            depth: 2000
 */
Behaviors.cameraBehavior = function cameraBehavior(behavior, component) {
    var camera = FamousConnector.getCamera();
    var payload = Behaviors.getPayload(behavior, component);
    if (!Array.isArray(payload)) {
        payload = [payload];
    }
    switch(behavior.name) {
        // TODO: need to figure out how to pass getters along to a component
        // case 'getValue':
        // case 'get-value':
        //     camera.getValue();
        //     break;
        case 'set':
            camera.set(payload[0], payload[1], payload[2], payload[3]);
            break;
        case 'setDepth':
        case 'set-depth':
            camera.setDepth(payload[0]);
            break;
        case 'setFlat':
        case 'set-flat':
            camera.setFlat();
            break;
        case 'setFrustum':
        case 'set-frustum':
            camera.setFrustum(payload[0], payload[1]);
            break;
        case 'setValue':
        case 'set-value':
            camera.setValue(payload[0]);
            break;
    }
};

/*
example:
famous:core:dom-element
    behaviors
        $self
            assign-content [searches own `$private` events, then `$public` events]
 */
Behaviors.selfMessagingBehavior = function selfMessagingBehavior(behavior, component) {
    var payload = Behaviors.getPayload(behavior, component);
    var event = component.events.getSelfEvent(behavior.name);
    component.events.triggerEvent(event, payload, component.uid);
};

/*
example:
famous:core:view
    behaviors
        $self
            famous:core:components:align
 */
Behaviors.selfDirectBehavior = function selfDirectBehavior(behavior, component) {
    var payload = Behaviors.getPayload(behavior, component);
    component.events.triggerDirectEventHandler(behavior.name, payload, component.uid);
};

/*
example:
famous:core:ui-element
    behaviors
        #view
            align
 */
Behaviors.targetMessagingBehavior = function targetMessagingBehavior(behavior, component, targets) {
{'fraction_non_alphanumeric': 0.11243896170650218, 'fraction_numerical': 0.001799023387304035, 'mean_word_length': 2.952767902488573, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4429268', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2117, 'n_words': 482}