import _extends from "@babel/runtime-corejs3/helpers/extends";
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
import _taggedTemplateLiteral from "@babel/runtime-corejs3/helpers/taggedTemplateLiteral";
var _templateObject;
import _spliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/splice";
import React from 'react';
import styled from 'styled-components';
import { internals } from '../../internals';
import { makeRecursiveMapper } from '../../utils';
import cx from 'classnames';
import { Classes } from '../../base/styles';
import { swapRTLDirection } from '../../base/utils';
var ExtendIconStyle = styled.span(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n  font-size:12px;\n  margin-", ":4px;\n"])), function (props) {
  return swapRTLDirection(props.direction, 'left');
});
var stateKey = 'colExtend';
var ExpandIcon = function ExpandIcon(_ref) {
  var style = _ref.style,
    className = _ref.className,
    size = _ref.size,
    isExtend = _ref.isExtend;
  return isExtend ? /*#__PURE__*/React.createElement("svg", {
    className: className,
    width: size,
    height: size,
    style: style,
    viewBox: "96 96 896 896"
  }, /*#__PURE__*/React.createElement("path", {
    d: "M678.347087 178.347087C690.550972 166.143203 690.550972 146.356797 678.347087 134.152913C666.143203 121.949029 646.356797 121.949029 634.152913 134.152913L290.402913 477.902913C278.199029 490.106797 278.199029 509.893203 290.402913 522.097087L634.152913 865.847087C646.356797 878.050972 666.143203 878.050972 678.347087 865.847087C690.550972 853.643203 690.550972 833.856797 678.347087 821.652913L356.694175 500L678.347087 178.347087z"
  })) : /*#__PURE__*/React.createElement("svg", {
    className: className,
    width: size,
    height: size,
    style: style,
    viewBox: "96 96 896 896"
  }, /*#__PURE__*/React.createElement("path", {
    d: "M321.652913 178.347087C309.449029 166.143203 309.449029 146.356797 321.652913 134.152913C333.856797 121.949029 353.643203 121.949029 365.847088 134.152913L709.597087 477.902913C721.800972 490.106797 721.800972 509.893203 709.597087 522.097087L365.847088 865.847087C353.643203 878.050972 333.856797 878.050972 321.652913 865.847087C309.449029 853.643203 309.449029 833.856797 321.652913 821.652913L643.305825 500L321.652913 178.347087z"
  }));
};
export var colGroupExtendable = function colGroupExtendable() {
  var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return function (pipeline) {
    var _a, _b;
    var columns = pipeline.getColumns();
    var curState = (_b = (_a = opts.extendStatus) !== null && _a !== void 0 ? _a : pipeline.getStateAtKey(stateKey)) !== null && _b !== void 0 ? _b : {};
    var processColumns = function processColumns(columns) {
      // 当组合列可伸缩，且处于收缩状态时，只渲染一个子列，其他不渲染
      var toggle = function toggle(col) {
        // 对应的 col 进行状态切换
        var changedValue = _defineProperty({}, col.code, !curState[col.code]);
        curState[col.code] = !curState[col.code];
        pipeline.setStateAtKey(stateKey, _extends({}, curState));
        (opts === null || opts === void 0 ? void 0 : opts.onChangeExtendStatus) && opts.onChangeExtendStatus(curState, changedValue);
      };
      var addIcon = function addIcon(col) {
        var result = _extends({}, col);
        var curColState = curState[col.code];
        var displaycolExtendIcon = typeof opts.extendIcon === 'function' ? opts.extendIcon(curColState) : opts.extendIcon;
        var addIconNode = /*#__PURE__*/React.createElement(React.Fragment, null, internals.safeRenderHeader(_extends({}, col)), /*#__PURE__*/React.createElement(ExtendIconStyle, {
          onClick: function onClick() {
            toggle(col);
          },
          direction: pipeline.ctx.direction
        }, displaycolExtendIcon || /*#__PURE__*/React.createElement(ExpandIcon, {
          style: {
            userSelect: 'none',
            marginLeft: 2,
            flexShrink: 0,
            cursor: 'pointer',
            verticalAlign: 'middle'
          },
          className: cx(_defineProperty({}, Classes.tableExtendIcon, true)),
          size: 14,
          isExtend: curColState
        })));
        result.title = addIconNode;
        return result;
      };
      return makeRecursiveMapper(function (col) {
        var _a;
        var _ref2 = (col === null || col === void 0 ? void 0 : col.features) || {},
          showExtendIcon = _ref2.showExtendIcon;
        if (showExtendIcon === true && ((_a = col.children) === null || _a === void 0 ? void 0 : _a.length) > 1) {
          var _context;
          col = addIcon(col);
          curState[col.code] === false && _spliceInstanceProperty(_context = col.children).call(_context, 1, col.children.length);
        }
        return col;
      })(columns);
    };
    pipeline.columns(processColumns(columns));
    return pipeline;
  };
};