import isEmpty from 'lodash/isEmpty';

function handleUpdate(_ref) {
  var name = _ref.name,
      value = _ref.value,
      record = _ref.record,
      allAppVersionDs = _ref.allAppVersionDs,
      tableDs = _ref.tableDs;

  if (name === 'app') {
    var _ref2 = value || {},
        _ref2$id = _ref2.id,
        id = _ref2$id === void 0 ? '' : _ref2$id,
        _ref2$appVersionVOS = _ref2.appVersionVOS,
        appVersionVOS = _ref2$appVersionVOS === void 0 ? [] : _ref2$appVersionVOS;

    tableDs.setQueryParameter('market_app_id', id);
    allAppVersionDs.loadData(appVersionVOS || []);

    if (isEmpty(appVersionVOS)) {
      record.set('market_app_version_id', null);
      tableDs.setQueryParameter('market_app_version_id', id);
    }
  } else {
    tableDs.setQueryParameter(name, value);
  }

  tableDs.query();
}

export default (function (_ref3) {
  var intlPrefix = _ref3.intlPrefix,
      formatMessage = _ref3.formatMessage,
      allAppVersionDs = _ref3.allAppVersionDs,
      allAppDs = _ref3.allAppDs,
      tableDs = _ref3.tableDs;
  return {
    autoCreate: true,
    autoQuery: false,
    paging: false,
    fields: [{
      name: 'app',
      type: 'object',
      textField: 'name',
      valueField: 'id',
      label: formatMessage({
        id: "".concat(intlPrefix)
      }),
      options: allAppDs
    }, {
      name: 'market_app_version_id',
      textField: 'versionNumber',
      valueField: 'id',
      label: formatMessage({
        id: "".concat(intlPrefix, ".version")
      }),
      options: allAppVersionDs
    }, {
      name: 'params',
      type: 'string'
    }],
    events: {
      update: function update(_ref4) {
        var name = _ref4.name,
            value = _ref4.value,
            record = _ref4.record;
        return handleUpdate({
          name: name,
          value: value,
          record: record,
          allAppVersionDs: allAppVersionDs,
          tableDs: tableDs
        });
      }
    }
  };
});