import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
import _extends from "@babel/runtime-corejs3/helpers/extends";
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
import React from 'react';
import { internals } from '../../internals';
import { always } from '../../utils/others';
import { mergeCellProps, SINGLE_SELECT_MARK_PROPNAME } from '../../utils';
export function singleSelect() {
  var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return function singleSelectStep(pipeline) {
    var _context;
    var _a, _b, _c, _d, _e, _f;
    var Radio = pipeline.ctx.components.Radio;
    if (Radio == null) {
      throw new Error('使用 singleSelect 之前需要通过 pipeline context 设置 components.Radio');
    }
    var stateKey = 'singleSelect';
    var clickArea = (_a = opts.clickArea) !== null && _a !== void 0 ? _a : 'radio';
    var isDisabled = (_b = opts.isDisabled) !== null && _b !== void 0 ? _b : always(false);
    var primaryKey = pipeline.ensurePrimaryKey('singleSelect');
    var value = (_d = (_c = opts.value) !== null && _c !== void 0 ? _c : pipeline.getStateAtKey(stateKey)) !== null && _d !== void 0 ? _d : opts.defaultValue;
    var onChange = function onChange(rowKey) {
      var _a;
      (_a = opts.onChange) === null || _a === void 0 ? void 0 : _a.call(opts, rowKey);
      pipeline.setStateAtKey(stateKey, rowKey);
    };
    var radioColumn = _extends(_extends({
      name: '',
      width: 50,
      align: 'center'
    }, opts.radioColumn), {
      getCellProps: function getCellProps(value, row, rowIndex) {
        var _a, _b;
        var preCellProps = (_b = (_a = opts.radioColumn) === null || _a === void 0 ? void 0 : _a.getCellProps) === null || _b === void 0 ? void 0 : _b.call(_a, value, row, rowIndex);
        if (clickArea === 'cell') {
          var rowKey = internals.safeGetRowKey(primaryKey, row, rowIndex);
          var disabled = isDisabled(row, rowIndex);
          return mergeCellProps(preCellProps, {
            style: {
              cursor: disabled ? 'not-allowed' : 'pointer'
            },
            onClick: disabled ? undefined : function (e) {
              if (opts.stopClickEventPropagation) {
                e.stopPropagation();
              }
              onChange(rowKey);
            }
          });
        }
        return preCellProps;
      },
      render: function render(_, row, rowIndex) {
        if (row[pipeline.getFeatureOptions('footerRowMetaKey')]) {
          return null;
        }
        var rowKey = internals.safeGetRowKey(primaryKey, row, rowIndex);
        return /*#__PURE__*/React.createElement(Radio, {
          checked: value === rowKey,
          disabled: isDisabled(row, rowIndex),
          onChange: clickArea === 'radio' ? function (arg1, arg2) {
            var _a;
            var nativeEvent = (_a = arg2 === null || arg2 === void 0 ? void 0 : arg2.nativeEvent) !== null && _a !== void 0 ? _a : arg1 === null || arg1 === void 0 ? void 0 : arg1.nativeEvent;
            if (nativeEvent && opts.stopClickEventPropagation) {
              nativeEvent.stopPropagation();
            }
            onChange(rowKey);
          } : undefined
        });
      },
      features: _extends(_extends({}, (_e = opts.radioColumn) === null || _e === void 0 ? void 0 : _e.features), _defineProperty({}, SINGLE_SELECT_MARK_PROPNAME, true))
    });
    var nextColumns = _sliceInstanceProperty(_context = pipeline.getColumns()).call(_context);
    var radioPlacement = (_f = opts.radioPlacement) !== null && _f !== void 0 ? _f : 'start';
    if (radioPlacement === 'start') {
      nextColumns.unshift(radioColumn);
    } else {
      nextColumns.push(radioColumn);
    }
    pipeline.columns(nextColumns);
    pipeline.appendRowPropsGetter(function (row, rowIndex) {
      var rowKey = internals.safeGetRowKey(primaryKey, row, rowIndex);
      var style = {};
      var className;
      var onClick;
      if (opts.highlightRowWhenSelected) {
        if (value === rowKey) {
          className = 'highlight';
        }
      }
      if (clickArea === 'row' && !isDisabled(row, rowIndex)) {
        style.cursor = 'pointer';
        onClick = function onClick(e) {
          if (opts.stopClickEventPropagation) {
            e.stopPropagation();
          }
          onChange(rowKey);
        };
      }
      return {
        className: className,
        style: style,
        onClick: onClick
      };
    });
    return pipeline;
  };
}