import { countBy, some } from 'lodash';

var setIndeterminate = function setIndeterminate(record) {
  var _record$get;

  record.set('indeterminate', false);
  (_record$get = record.get('childrenDataSet')) === null || _record$get === void 0 ? void 0 : _record$get.forEach(function (eachRecord) {
    eachRecord.set('indeterminate', false);
  });
};

export default (function (_ref) {
  var intlPrefix = _ref.intlPrefix,
      formatMessage = _ref.formatMessage,
      selectedDs = _ref.selectedDs,
      setAppChildren = _ref.setAppChildren,
      setVersionChildren = _ref.setVersionChildren,
      handleMarketServiceCheck = _ref.handleMarketServiceCheck,
      handleAppExpand = _ref.handleAppExpand,
      handleVersionExpand = _ref.handleVersionExpand;
  return {
    autoQuery: true,
    pageSize: 10,
    expandField: 'expand',
    transport: {
      read: {
        url: 'market/v1/market/application/market_application_with_sourcecode/page',
        method: 'get'
      }
    },
    fields: [{
      name: 'name',
      label: formatMessage({
        id: 'name'
      })
    }, {
      name: 'sourceProject',
      label: formatMessage({
        id: "".concat(intlPrefix, ".source")
      })
    }, {
      name: 'lastUpdateDate',
      label: formatMessage({
        id: 'updateDate'
      })
    }],
    events: {
      select: function select(_ref2) {
        var record = _ref2.record;
        // eslint-disable-next-line no-param-reassign
        record.isSelected = true;
        setAppChildren(record);
        record.get('childrenDataSet').selectAll();
        setIndeterminate(record);
      },
      unSelect: function unSelect(_ref3) {
        var record = _ref3.record;
        // eslint-disable-next-line no-param-reassign
        record.isSelected = false;

        if (record.get('childrenDataSet')) {
          record.get('childrenDataSet').unSelectAll();
        }

        setIndeterminate(record);
      },
      selectAll: function selectAll(_ref4) {
        var dataSet = _ref4.dataSet;
        dataSet.forEach(function (record) {
          setAppChildren(record);
          record.get('childrenDataSet').selectAll();
          setIndeterminate(record);
        });
      },
      unSelectAll: function unSelectAll(_ref5) {
        var dataSet = _ref5.dataSet;
        dataSet.forEach(function (record) {
          if (record.get('childrenDataSet')) {
            record.get('childrenDataSet').unSelectAll();
          }

          setIndeterminate(record);
        });
      },
      load: function load(_ref6) {
        var dataSet = _ref6.dataSet;

        if (!dataSet.length) {
          return;
        }

        if (selectedDs.length) {
          var selectedData = selectedDs.toData();
          var appIdObject = countBy(selectedData, 'marketAppId');
          var appVersionIdObject = countBy(selectedData, 'marketAppVersionId');
          dataSet.forEach(function (record) {
            if (some(appIdObject, function (value, key) {
              return record.get('id') === key;
            })) {
              var _record$get2;

              setAppChildren(record);
              (_record$get2 = record.get('childrenDataSet')) === null || _record$get2 === void 0 ? void 0 : _record$get2.forEach(function (eachRecord) {
                if (some(appVersionIdObject, function (value, key) {
                  return eachRecord.get('id') === key;
                })) {
                  var _eachRecord$get;

                  setVersionChildren(eachRecord);
                  (_eachRecord$get = eachRecord.get('childrenDataSet')) === null || _eachRecord$get === void 0 ? void 0 : _eachRecord$get.forEach(function (childRecord) {
                    if (selectedDs.some(function (selectedRecord) {
                      return selectedRecord.get('deployObjectId') === childRecord.get('deployObjectId') && selectedRecord.get('marketAppId') === record.get('id') && selectedRecord.get('marketAppVersionId') === eachRecord.get('id');
                    })) {
                      handleMarketServiceCheck({
                        checked: true,
                        childRecord: childRecord,
                        childDs: eachRecord.get('childrenDataSet'),
                        parentDs: record.get('childrenDataSet'),
                        parentRecord: eachRecord,
                        record: record
                      });
                    }
                  });
                }
              });
            }
          });
        }

        var record = dataSet.get(0);

        if (record) {
          record.set('expand', true);
          handleAppExpand(true, record);
          var childDs = record.get('childrenDataSet');
          var childRecord = childDs.get(0);

          if (childRecord) {
            handleVersionExpand(childRecord);
          }
        }
      }
    }
  };
});