{"size": 1122, "ext": "js", "max_stars_repo_path": "src/vendor/temando/module-shipping-m2/view/frontend/web/js/model/fields-definition.js", "max_stars_repo_name": "tuliodemelo/fulcrum", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/vendor/temando/module-shipping-m2/view/frontend/web/js/model/fields-definition.js", "max_issues_repo_name": "tuliodemelo/fulcrum", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/vendor/temando/module-shipping-m2/view/frontend/web/js/model/fields-definition.js", "max_forks_repo_name": "tuliodemelo/fulcrum", "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.5, "max_line_length": 90, "alphanum_fraction": 0.513368984}
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */

define([
    'underscore',
    'ko',
    'Magento_Customer/js/customer-data'
], function (_, ko, customerData) {
    'use strict';

    var cacheKey = 'checkout-fields';

    return {
        getFields: function () {
            var sectionData = customerData.get(cacheKey);

            _.each(sectionData().fields, function (field) {
                if ((field.value === undefined) && field.defaultValue) {
                    field.value = field.defaultValue;
                }
            });

            return sectionData().fields;
        },

        updateFieldValue: function (fieldId, fieldValue) {
            var sectionData = customerData.get(cacheKey);

            if (fieldValue === undefined) {
                fieldValue = '';
            }

            _.each(sectionData().fields, function (field) {
                if (field.id === fieldId) {
                    field.value = fieldValue;
                }
            });

            customerData.set(cacheKey, sectionData());
        }
    };
});
