{"size": 3550, "ext": "js", "max_stars_repo_path": "client/js/services/display-logic-digest.service.js", "max_stars_repo_name": "hunterBhough/epahomeratingapp-ui", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/js/services/display-logic-digest.service.js", "max_issues_repo_name": "hunterBhough/epahomeratingapp-ui", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/js/services/display-logic-digest.service.js", "max_forks_repo_name": "hunterBhough/epahomeratingapp-ui", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.9124087591, "max_line_length": 103, "alphanum_fraction": 0.4890140845}
import _cloneDeep from 'lodash/cloneDeep';

const DIGEST_TYPE = {
    ENERGY : 'ENERGYGAUGE',
    REM    : 'REMRATE'
};

class DisplayLogicDigestService {
    constructor ($http, $q, API_URL, FootNotesService, S3Service) {
        'ngInject';

        this.$http = $http;
        this.$q = $q;
        this.S3Service = S3Service;

        this.FootNotesService = FootNotesService;

        this.API_URL = API_URL;

        this.digestEnergy = this.$http({
            method  : 'GET',
            url     : `${this.API_URL.DISPLAY_LOGIC_DIGEST}/energy`
        });
        this.digestRem = this.$http({
            method  : 'GET',
            url     : `${this.API_URL.DISPLAY_LOGIC_DIGEST}/rem`
        });

        this.digestRem.then(digest => {
            this.syncDigest = digest;
        });

    }

    setDigest (type) {
        this.digest = this.getDigest(type);
    }

    getDigest (xmlType) {
        let digestPromise;
        switch (xmlType) {
        case DIGEST_TYPE.ENERGY:
            digestPromise = this.digestEnergy;
            break;
        case DIGEST_TYPE.REM:
            digestPromise = this.digestRem;
            break;
        default:
            digestPromise = this.$q();
        }
        return digestPromise;
    }

    getPromise (xmlType) {
        switch (xmlType) {
        case DIGEST_TYPE.ENERGY:
            return this.digestEnergy;
        case DIGEST_TYPE.REM:
            return this.digestRem;
        default:
            return this.digestRem;
        }
    }

    get (id) {
        let checklistItemDisplay = this.digest.then(digest => {
            let checklistItem = _cloneDeep(digest.data.ChecklistItems[id]);
            if (checklistItem) { // TODO? find a better solution than ignoring undefined checklistItems
                checklistItem.Footnotes = this.FootNotesService.fetchData(id).Footnotes;
            }
            return checklistItem;
        });

        return checklistItemDisplay;
    }

    getSync (id) {
        return this.syncDigest.data.ChecklistItems[id];
    }

    getEnum (Name) {
        return this.$q((resolve, reject) => {
            this.digest.then(digest => {
                if (digest.data.Enums[Name]) {
                    resolve(digest.data.Enums[Name]);
                } else {
                    reject('Not Found');
                }
            });
        });
    }

    getDecimal (Name) {
        return this.$q((resolve, reject) => {
            this.digest.then(digest => {
                if (digest.data.Decimals[Name]) {
                    resolve(digest.data.Decimals[Name]);
                } else {
                    reject('Not Found');
                }
            });
        });
    }

    getInteger (Name) {
        return this.$q((resolve, reject) => {
            this.digest.then(digest => {
                if (digest.data.Integers[Name]) {
                    resolve(digest.data.Integers[Name]);
                } else {
                    reject('Not Found');
                }
            });
        });
    }

    getString (Name) {
        return this.$q((resolve, reject) => {
            this.digest.then(digest => {
                if (digest.data.Strings[Name]) {
                    resolve(digest.data.Strings[Name]);
                } else {
                    reject('Not Found');
                }
            });
        });
    }

    getOrder () {
        return this.digest.then(digest => {
            return digest.data.Order;
        });
    }
}

export default DisplayLogicDigestService;
