'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.stimulusSchema = exports.sessionItemSchema = exports.sessionItemResultSchema = exports.shareBankSchema = exports.resumeDataSchema = exports.quizSessionSchema = exports.quizSessionResultSchema = exports.quizSessionEventSchema = exports.quizSchema = exports.quizEntrySchema = exports.quizEntriesSchema = exports.quizAnalysisSchema = exports.qtiImportSchema = exports.itemSchema = exports.itemAnalysisSchema = exports.interactionTypeSchema = exports.bankSchema = exports.bankEntrySchema = undefined;

var _normalizr = require('normalizr');

var bankSchema = new _normalizr.schema.Entity('banks');
var bankEntrySchema = new _normalizr.schema.Entity('bankEntries');
var interactionTypeSchema = new _normalizr.schema.Entity('interactionTypes');
var itemAnalysisSchema = new _normalizr.schema.Entity('itemAnalyses', {}, { idAttribute: 'item_id' });
var quizAnalysisSchema = new _normalizr.schema.Entity('quizAnalyses', {}, { idAttribute: 'quiz_id' });
var itemSchema = new _normalizr.schema.Entity('items');
var qtiImportSchema = new _normalizr.schema.Entity('qtiImports');
var quizSchema = new _normalizr.schema.Entity('quizzes');
var quizSessionEventSchema = new _normalizr.schema.Entity('quizSessionEvents', {}, {
  idAttribute: 'created_at' // indexed by createdAt
});

var shareBankSchema = new _normalizr.schema.Entity('sharedBanks');
var quizSessionResultSchema = new _normalizr.schema.Entity('quizSessionResults');
var quizSessionSchema = new _normalizr.schema.Entity('quizSessions');
var quizEntrySchema = new _normalizr.schema.Entity('quizEntry');
var quizEntriesSchema = new _normalizr.schema.Entity('quizEntries');
var resumeDataSchema = new _normalizr.schema.Entity('resumeData');
var sessionItemResultSchema = new _normalizr.schema.Entity('sessionItemResults');
var sessionItemSchema = new _normalizr.schema.Entity('sessionItems');
var stimulusSchema = new _normalizr.schema.Entity('stimuli');

// =================
//   relationships
// =================

bankEntrySchema.define({
  entry: new _normalizr.schema.Union({
    Item: itemSchema,
    Stimulus: stimulusSchema
  }, function (value, parent, key) {
    return parent.entry_type;
  })
});

itemSchema.define({
  interaction_type: interactionTypeSchema
});

stimulusSchema.define({});

quizSchema.define({
  items: [itemSchema], // Probably shouldn't have this at the root anymore...
  quizEntries: [quizEntrySchema],
  itemAnalyses: [itemAnalysisSchema],
  quizAnalysis: quizAnalysisSchema
});

var quizEntryNestedDef = {
  entry: new _normalizr.schema.Union({
    Bank: bankSchema,
    BankEntry: bankEntrySchema,
    Item: itemSchema,
    Stimulus: stimulusSchema
  }, function (value, parent, key) {
    return parent.entry_type;
  })
};

quizEntrySchema.define(quizEntryNestedDef);
quizEntriesSchema.define(quizEntryNestedDef);

// 'item' is within the sessionItem, but it should remain
// denormalized because sessionItems of the same id can
// actually represent different objects
// however we do want to normalize the interaction types
// within it
sessionItemSchema.define({
  item: {
    interaction_type: interactionTypeSchema
  }
});

sessionItemResultSchema.define({
  quiz_session: quizSessionSchema,
  session_items: [sessionItemSchema]
});

quizSessionSchema.define({
  authoritative_result: quizSessionResultSchema,
  quiz: quizSchema,
  quiz_session_results: [quizSessionResultSchema]
});

quizSessionEventSchema.define({
  quiz_session: quizSessionSchema
});

exports.bankEntrySchema = bankEntrySchema;
exports.bankSchema = bankSchema;
exports.interactionTypeSchema = interactionTypeSchema;
exports.itemAnalysisSchema = itemAnalysisSchema;
exports.itemSchema = itemSchema;
exports.qtiImportSchema = qtiImportSchema;
exports.quizAnalysisSchema = quizAnalysisSchema;
exports.quizEntriesSchema = quizEntriesSchema;
exports.quizEntrySchema = quizEntrySchema;
exports.quizSchema = quizSchema;
exports.quizSessionEventSchema = quizSessionEventSchema;
exports.quizSessionResultSchema = quizSessionResultSchema;
exports.quizSessionSchema = quizSessionSchema;
exports.resumeDataSchema = resumeDataSchema;
exports.shareBankSchema = shareBankSchema;
exports.sessionItemResultSchema = sessionItemResultSchema;
exports.sessionItemSchema = sessionItemSchema;
exports.stimulusSchema = stimulusSchema;