define([
  'app',
  'modules/catalog-code/catalog-code',
],
function (app, CatalogCode) {

    var Main = {
        Models: {},
        Collections: {},
    };

    (function () {
        var superRef, superProto, classRef;

        superRef = CatalogCode.Models.CatalogCode;
        superProto = superRef.prototype;

        Main.Models.ChemistrySampleId = classRef = superRef.extend({
            defaults: _.extend(
                {},
                superProto.defaults,
                {
                    chemistrySampleTypeCode: null,
                }
            ),
            procedures: {
                get: app.DatabaseNames.MES + '.CAT.GetChemistrySampleIds',
            },

            parse: function (obj) {
                return _.extend(superProto.parse.apply(this, arguments), {
                    chemistrySampleTypeCode: obj.ChemistrySampleTypeCode,
                });
            },
        });
    })();

    (function () {
        var superRef, superProto, classRef;

        superRef = CatalogCode.Collections.CatalogCodes;
        superProto = superRef.prototype;

        Main.Collections.ChemistrySampleIds = classRef = superRef.extend({
            model: Main.Models.ChemistrySampleId,
        });
        _.extend(classRef, {
            fetch: function (options) {
                var opt = _.extend(
                        { error: function () { }, success: function () { }, },
                        options
                    );

                classRef.prototype.getData.apply(classRef.prototype, [_.extend(
                    {},
                    opt,
                    {
                        success: function (resp) {
                            if (resp.Table) {
                                opt.success(_.map(resp.Table, classRef.prototype.model.prototype.parse));
                            }
                            else {
                                var msg = (resp.Message) ? resp.Message : 'Can not fetch chemistry sample ids.';

                                console.error(new Error(msg).stack);
                                opt.error(msg);
                            }
                        },
                    }
                )]);
            },
        });
    })();

    // Required, return the module for AMD compliance.
    return Main;

});