import { defineComponent, inject, createVNode, Fragment } from 'vue';
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { provideKey } from '../const';
import Td from './td';
import ExpandTr from './expandTr';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var tr = defineComponent({
  components: {
    ExpandTr,
    Td
  },
  props: {
    row: {
      type: Object
    },
    rowIndex: {
      type: Number
    },
    columns: {
      type: Array,
      required: true
    },
    expanded: {
      type: Boolean,
      default: true
    }
  },
  setup(props) {
    const {
      handleRowClick,
      getRowStyle,
      getRowClassName,
      expandColumn,
      isExpandOpened,
      handleCellClick,
      getCellValue
    } = inject(provideKey);
    const renderTdList = (row, rowIndex) => props.columns.map((column, columnIndex) => createVNode(Td, {
      "key": column.id,
      "row": row,
      "rowIndex": rowIndex,
      "column": column,
      "columnIndex": columnIndex,
      "columns": props.columns,
      "onClick": $event => {
        handleCellClick({
          row,
          column,
          cellValue: getCellValue(row, column)
        }, $event);
      }
    }, null));
    const renderTr = () => {
      const {
        row,
        rowIndex
      } = props;
      return createVNode("tr", {
        "class": getRowClassName({
          row,
          rowIndex
        }),
        "style": _objectSpread({}, getRowStyle({
          row,
          rowIndex
        })),
        "onClick": $event => {
          handleRowClick({
            row,
            rowIndex
          }, $event);
        }
      }, [renderTdList(row, rowIndex)]);
    };
    return () => {
      const {
        row,
        rowIndex,
        expanded
      } = props;
      if (!expanded) {
        return renderTr();
      }
      return createVNode(Fragment, null, [renderTr(), expandColumn.value && isExpandOpened({
        row
      }) && createVNode(ExpandTr, {
        "row": row,
        "column": expandColumn.value,
        "rowIndex": rowIndex,
        "length": props.columns.length
      }, null)]);
    };
  }
});

export { tr as default };
