function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import isEmpty from 'lodash/isEmpty';
import pick from 'lodash/pick';

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

  switch (name) {
    case 'chartCustom':
      if (value === 'default') {
        record.set('chartStatus', '');
      }

      break;

    case 'userName':
    case 'password':
    case 'url':
      record.set('chartStatus', '');
      break;

    default:
      break;
  }
}

function handleLoad(_ref2) {
  var dataSet = _ref2.dataSet;
  var record = dataSet.current;

  if (!record) {
    return;
  }

  var chart = record.get('chart');

  if (!isEmpty(chart)) {
    var _ref3 = chart.config || {},
        url = _ref3.url,
        userName = _ref3.userName,
        password = _ref3.password;

    record.init('chartCustom', 'custom');
    record.init('url', url);
    record.init('userName', userName);
    record.init('password', password);
  } else {
    record.init('chartCustom', 'default');
  }
}

function getRequestData(data, res) {
  var url = data.url,
      userName = data.userName,
      password = data.password,
      chartCustom = data.chartCustom;

  if (chartCustom === 'custom') {
    if (isEmpty(res.chart)) {
      res.chart = {
        type: 'chart',
        config: {}
      };
    }

    res.chart.custom = true;
    res.chart.config.url = url;
    res.chart.config.userName = userName;
    res.chart.config.password = password;
  } else {
    res.chart = null;
  }
}

export default (function (intlPrefix, formatMessage, url) {
  function checkUserName(value, name, record) {
    if (!value && record.get('password')) {
      return formatMessage({
        id: "".concat(intlPrefix, ".name.check.failed")
      });
    }
  }

  function checkPassword(value, name, record) {
    if (!value && record.get('userName')) {
      return formatMessage({
        id: "".concat(intlPrefix, ".name.check.failed")
      });
    }
  }

  return {
    autoQuery: true,
    selection: false,
    paging: false,
    dataKey: null,
    autoQueryAfterSubmit: false,
    transport: {
      read: {
        url: url,
        method: 'get'
      },
      update: function update(_ref4) {
        var _ref4$data = _slicedToArray(_ref4.data, 1),
            data = _ref4$data[0];

        var res = pick(data, ['chart']);
        getRequestData(data, res);
        return {
          url: url,
          method: 'post',
          data: res
        };
      }
    },
    fields: [{
      name: 'chartCustom',
      type: 'string',
      defaultValue: 'default'
    }, {
      name: 'chart',
      type: 'object'
    }, {
      name: 'url',
      type: 'url',
      label: formatMessage({
        id: 'address'
      }),
      dynamicProps: {
        required: function required(_ref5) {
          var record = _ref5.record;
          return record.get('chartCustom') === 'custom';
        }
      }
    }, {
      name: 'userName',
      type: 'string',
      label: formatMessage({
        id: 'userName'
      }),
      validator: checkUserName
    }, {
      name: 'password',
      type: 'string',
      label: formatMessage({
        id: 'password'
      }),
      validator: checkPassword
    }, {
      name: 'chartStatus',
      type: 'string',
      defaultValue: ''
    }],
    events: {
      update: handleUpdate,
      load: handleLoad
    }
  };
});