"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.treeSelect = treeSelect;

var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));

var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _others = require("../../utils/others");

var _StrictTreeDataHelper = _interopRequireDefault(require("../../utils/tree-data-helpers/StrictTreeDataHelper"));

var _TreeDataHelper = _interopRequireDefault(require("../../utils/tree-data-helpers/TreeDataHelper"));

var STATE_KEY = 'treeSelect';

function treeSelect(opts) {
  return function treeSelectStep(pipeline) {
    var _ref, _context;

    var _a, _b, _c, _d, _e, _f, _g, _h;

    var Checkbox = pipeline.ctx.components.Checkbox;

    if (Checkbox == null) {
      throw new Error('使用 treeSelect 之前需要通过 pipeline context 设置 components.Checkbox');
    }

    var primaryKey = pipeline.ensurePrimaryKey('treeSelect');

    if (typeof primaryKey !== 'string') {
      throw new Error('treeSelect 仅支持字符串作为 primaryKey');
    }

    var clickArea = (_a = opts.clickArea) !== null && _a !== void 0 ? _a : 'checkbox';
    var isDisabled = (_b = opts.isDisabled) !== null && _b !== void 0 ? _b : (0, _others.always)(false);
    var isDetached = (_c = opts.idDetached) !== null && _c !== void 0 ? _c : (0, _others.always)(false);
    var value = (_f = (_e = (_d = opts.value) !== null && _d !== void 0 ? _d : pipeline.getStateAtKey(STATE_KEY)) !== null && _e !== void 0 ? _e : opts.defaultValue) !== null && _f !== void 0 ? _f : [];
    var tree = opts.rootKey != null ? [(_ref = {}, (0, _defineProperty2.default)(_ref, primaryKey, opts.rootKey), (0, _defineProperty2.default)(_ref, "children", opts.tree), _ref)] : opts.tree;

    var getNodeValue = function getNodeValue(node) {
      return node[primaryKey];
    };

    var treeDataHelper = opts.checkStrictly ? new _StrictTreeDataHelper.default({
      value: value,
      getNodeValue: getNodeValue,
      tree: tree
    }) : new _TreeDataHelper.default({
      value: value,
      getNodeValue: getNodeValue,
      isDetached: isDetached,
      tree: tree,
      checkedStrategy: (_g = opts.checkedStrategy) !== null && _g !== void 0 ? _g : 'parent'
    });

    var onToggleKey = function onToggleKey(key) {
      var _a;

      var nextValue = treeDataHelper.getValueAfterToggle(key);
      pipeline.setStateAtKey(STATE_KEY, nextValue);
      (_a = opts.onChange) === null || _a === void 0 ? void 0 : _a.call(opts, nextValue);
    };

    var makeCheckbox = function makeCheckbox(key, root, row) {
      return /*#__PURE__*/_react.default.createElement(Checkbox, {
        checked: treeDataHelper.isChecked(key),
        indeterminate: treeDataHelper.isIndeterminate(key),
        disabled: !root && isDisabled(row),
        onChange: clickArea === 'checkbox' || root ? function () {
          return onToggleKey(key);
        } : undefined
      });
    };

    var checkboxColumn = (0, _extends2.default)((0, _extends2.default)({
      name: '',
      width: 50,
      align: 'center',
      title: opts.rootKey != null ? makeCheckbox(opts.rootKey, true) : undefined
    }, opts.checkboxColumn), {
      render: function render(value, record) {
        return makeCheckbox(record[primaryKey], false, record);
      },
      getCellProps: function getCellProps(value, row) {
        var rowKey = row[primaryKey];

        if (clickArea !== 'cell') {
          return;
        }

        var disabled = isDisabled(row);

        if (disabled) {
          return {
            style: {
              cursor: 'not-allowed'
            }
          };
        }

        return {
          style: {
            cursor: 'pointer'
          },
          onClick: function onClick(e) {
            if (opts.stopClickEventPropagation) {
              e.stopPropagation();
            }

            onToggleKey(rowKey);
          }
        };
      }
    });
    var nextColumns = (0, _slice.default)(_context = pipeline.getColumns()).call(_context);
    var checkboxPlacement = (_h = opts.checkboxPlacement) !== null && _h !== void 0 ? _h : 'start';

    if (checkboxPlacement === 'start') {
      nextColumns.unshift(checkboxColumn);
    } else {
      nextColumns.push(checkboxColumn);
    }

    pipeline.columns(nextColumns);

    if (clickArea === 'row') {
      pipeline.appendRowPropsGetter(function (row) {
        var disabled = isDisabled(row);

        if (!disabled) {
          return {
            style: {
              cursor: 'pointer'
            },
            onClick: function onClick(e) {
              if (opts.stopClickEventPropagation) {
                e.stopPropagation();
              }

              onToggleKey(row[primaryKey]);
            }
          };
        }
      });
    }

    if (opts.highlightRowWhenSelected) {
      pipeline.appendRowPropsGetter(function (row) {
        if (treeDataHelper.isChecked(row[primaryKey])) {
          return {
            className: 'highlight'
          };
        }
      });
    }

    return pipeline;
  };
}