import React from 'react';
import dayjs from 'dayjs';
import { Button } from '@pisell/materials';
import { STATUS_TEXT_MAP, STATUS_COLOR_MAP } from "../const";
import MoreDropdown from "../../components/MoreDropdown";
import { useIsApp } from "../../hooks";
import "../index.less";
var prefixCls = 'ecocup-cup-list';
export var useColumns = function useColumns(props) {
  var onClickAction = props.onClickAction,
    locales = props.locales,
    isEcoShare = props.isEcoShare;
  var isApp = useIsApp();
  var getRelativeTime = function getRelativeTime(text) {
    if (!text) return '';
    var diff = dayjs().diff(dayjs(text), 'day');
    if (diff === 0) return locales.getText('pisell2.text.ecocup.table.time-of-return.today');
    return locales.getText('pisell2.text.ecocup.table.time-of-rent.relative')(diff);
  };
  return [{
    title: locales.getText('pisell2.text.ecocup.table.cup-code'),
    dataIndex: 'eco_cup_code',
    key: 'eco_cup_code',
    align: 'center',
    width: 170,
    render: function render(text, record) {
      return text || (record === null || record === void 0 ? void 0 : record.code) || '';
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.table.customer'),
    dataIndex: 'customer',
    key: 'customer',
    align: 'center',
    hidden: isApp,
    with: 220,
    render: function render(text, record) {
      var _record$customer;
      return (record === null || record === void 0 || (_record$customer = record.customer) === null || _record$customer === void 0 ? void 0 : _record$customer.display_name) || '';
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.table.status'),
    dataIndex: 'status',
    key: 'status',
    align: 'center',
    width: 120,
    render: function render(text) {
      var status = STATUS_TEXT_MAP[text];
      var color = STATUS_COLOR_MAP[text];
      return /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-table-status"),
        style: {
          backgroundColor: color === null || color === void 0 ? void 0 : color.backgroundColor,
          color: color === null || color === void 0 ? void 0 : color.color,
          border: "1px solid ".concat(color === null || color === void 0 ? void 0 : color.border)
        }
      }, locales.getText(status));
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.table.shop-of-rent'),
    dataIndex: 'rent_shop',
    key: 'rent_shop',
    align: 'center',
    hidden: isApp,
    width: 200,
    render: function render(text, record) {
      var _record$rent_shop;
      return (record === null || record === void 0 || (_record$rent_shop = record.rent_shop) === null || _record$rent_shop === void 0 ? void 0 : _record$rent_shop.name) || '';
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.table.time-of-rent'),
    dataIndex: 'rent_time',
    key: 'rent_time',
    align: 'center',
    hidden: isApp,
    width: 160,
    render: function render(text) {
      if (!text) return '';
      return /*#__PURE__*/React.createElement("div", {
        style: {
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center'
        }
      }, /*#__PURE__*/React.createElement("span", null, dayjs(text).format('DD/MM/YYYY')), /*#__PURE__*/React.createElement("span", null, getRelativeTime(text)));
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.table.shop-of-return'),
    dataIndex: 'return_shop',
    key: 'return_shop',
    align: 'center',
    hidden: isApp,
    width: 200,
    render: function render(text, record) {
      var _record$return_shop;
      return (record === null || record === void 0 || (_record$return_shop = record.return_shop) === null || _record$return_shop === void 0 ? void 0 : _record$return_shop.name) || '';
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.table.time-of-return'),
    dataIndex: 'return_time',
    key: 'return_time',
    align: 'center',
    hidden: isApp,
    width: 160,
    render: function render(text) {
      return text ? dayjs(text).format('DD/MM/YYYY') : '';
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.rent'),
    dataIndex: 'rent_shop',
    key: 'rent_shop',
    align: 'center',
    hidden: !isApp,
    width: 160,
    render: function render(text, record) {
      var _record$rent_shop2, _record$rent_shop3;
      return /*#__PURE__*/React.createElement("div", {
        style: {
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center'
        }
      }, record !== null && record !== void 0 && (_record$rent_shop2 = record.rent_shop) !== null && _record$rent_shop2 !== void 0 && _record$rent_shop2.name ? /*#__PURE__*/React.createElement("span", null, record === null || record === void 0 || (_record$rent_shop3 = record.rent_shop) === null || _record$rent_shop3 === void 0 ? void 0 : _record$rent_shop3.name) : null, record !== null && record !== void 0 && record.rent_time ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", null, dayjs(record === null || record === void 0 ? void 0 : record.rent_time).format('DD/MM/YYYY')), /*#__PURE__*/React.createElement("span", null, getRelativeTime(record === null || record === void 0 ? void 0 : record.rent_time))) : null);
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.action.return'),
    dataIndex: 'return_shop',
    key: 'return_shop',
    align: 'center',
    hidden: !isApp,
    width: 160,
    render: function render(text, record) {
      var _record$return_shop2, _record$return_shop3;
      return /*#__PURE__*/React.createElement("div", {
        style: {
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center'
        }
      }, record !== null && record !== void 0 && (_record$return_shop2 = record.return_shop) !== null && _record$return_shop2 !== void 0 && _record$return_shop2.name ? /*#__PURE__*/React.createElement("span", null, record === null || record === void 0 || (_record$return_shop3 = record.return_shop) === null || _record$return_shop3 === void 0 ? void 0 : _record$return_shop3.name) : null, record !== null && record !== void 0 && record.return_time ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", null, dayjs(record === null || record === void 0 ? void 0 : record.return_time).format('DD/MM/YYYY')), /*#__PURE__*/React.createElement("span", null, getRelativeTime(record === null || record === void 0 ? void 0 : record.return_time))) : null);
    }
  }, {
    title: locales.getText('pisell2.text.ecocup.table.actions'),
    dataIndex: 'actions',
    key: 'actions',
    hidden: isApp || isEcoShare,
    width: 180,
    render: function render(text, record) {
      var _status = (record === null || record === void 0 ? void 0 : record.status) || '';
      var showMoreTypes = ['in_stock', 'rent'];
      if ((record === null || record === void 0 ? void 0 : record.shop_id) !== (record === null || record === void 0 ? void 0 : record.current_shop_id) && !(record !== null && record !== void 0 && record.eco_cup_id)) return null;
      return /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-table-actions")
      }, _status === 'rent' ? /*#__PURE__*/React.createElement(Button, {
        type: "primary",
        onClick: function onClick() {
          return onClickAction('return', record);
        }
      }, locales.getText('pisell2.text.ecocup.action.return')) : null, showMoreTypes.includes(_status) ? /*#__PURE__*/React.createElement(MoreDropdown, {
        locales: locales,
        onClick: function onClick(key) {
          return onClickAction(key, record);
        }
      }) : null);
    }
  }];
};