var __extends = this && this.__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");

    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var _a;

import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import { RangePicker as RCRangePicker } from 'rc-picker';
import { BaseContext, LocaleEnum } from "../../style/context";
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import 'dayjs/locale/en';
import en from "../languages/en-US";
import zh from "../languages/zh-CN";
import { getRangePlaceholder, TimeFormat, getTimeFormatPlaceholder } from "../util";
import Icon from "../../icon/Icon";
import RadioGroup from "../../radio/RadioGroup";
import { getTimeProps, PickerComponents } from "./";
var localeMap = (_a = {}, _a[LocaleEnum.en] = en, _a[LocaleEnum.zh] = zh, _a);
var prefixCls = 'dmc-date-picker';
export default function generateRangePicker(generateConfig) {
  var RangePicker =
  /** @class */
  function (_super) {
    __extends(RangePicker, _super);

    function RangePicker(props) {
      var _a;

      var _this = _super.call(this, props) || this;

      _this.pickerRef = /*#__PURE__*/React.createRef();

      _this.focus = function () {
        if (_this.pickerRef.current) {
          _this.pickerRef.current.focus();
        }
      };

      _this.blur = function () {
        if (_this.pickerRef.current) {
          _this.pickerRef.current.blur();
        }
      };

      _this.getDefaultLocale = function () {
        var _a = _this.props.locale,
            locale = _a === void 0 ? {} : _a;
        var language = _this.context.locale || LocaleEnum.zh; // eslint-disable-next-line @typescript-eslint/no-var-requires

        var local = localeMap[language];
        dayjs.locale(language);

        var result = __assign(__assign({}, local), locale);

        result.lang = __assign(__assign({}, result.lang), (locale || {}).lang);
        return result;
      };

      _this.handleRadioChange = function (val) {
        _this.setState({
          timeFormat: val
        });
      };

      _this._renderExtraFooter = function (mode) {
        if (mode === 'date') {
          var local_1 = _this.getDefaultLocale();

          var options = [TimeFormat.hm, TimeFormat.hms].map(function (t) {
            return {
              value: t,
              label: getTimeFormatPlaceholder(t, local_1)
            };
          });
          return _jsx(RadioGroup, {
            value: _this.state.timeFormat,
            options: options,
            className: prefixCls + "-footer-range-radio-group",
            onChange: _this.handleRadioChange
          }, void 0);
        }

        var renderExtraFooter = _this.props.renderExtraFooter;

        if (renderExtraFooter) {
          return renderExtraFooter(mode);
        }
      };

      _this.renderPicker = function (locale) {
        var _a;

        var _b = _this.props,
            className = _b.className,
            _c = _b.bordered,
            bordered = _c === void 0 ? true : _c,
            placeholder = _b.placeholder,
            picker = _b.picker,
            type = _b.type,
            size = _b.size,
            restProps = __rest(_b, ["className", "bordered", "placeholder", "picker", "type", "size"]);

        var _d = _this.props,
            format = _d.format,
            showTime = _d.showTime;
        var timeFormat = _this.state.timeFormat;

        if (showTime) {
          if (timeFormat === TimeFormat.hm) {
            showTime = true;
            format = 'HH:mm';
          } else if (timeFormat === TimeFormat.hms) {
            showTime = true;
            format = 'HH:mm:ss';
          }
        }

        var additionalOverrideProps = {};
        additionalOverrideProps = __assign(__assign(__assign(__assign({}, additionalOverrideProps), showTime ? getTimeProps(__assign({
          format: format,
          picker: picker
        }, showTime)) : {}), picker === 'time' ? getTimeProps(__assign(__assign({
          format: format
        }, _this.props), {
          picker: picker
        })) : {}), showTime ? {
          renderExtraFooter: _this._renderExtraFooter
        } : {});
        return _jsx(RCRangePicker, __assign({
          separator: _jsx("span", __assign({
            "aria-label": "to",
            className: prefixCls + "-separator"
          }, {
            children: _jsx(Icon, {
              className: prefixCls + "-separator-icon",
              icon: "remove"
            }, void 0)
          }), void 0),
          ref: _this.pickerRef,
          placeholder: getRangePlaceholder(picker, locale, placeholder),
          suffixIcon: picker === 'time' ? _jsx(Icon, {
            className: prefixCls + "-suffix-icon",
            icon: "time-clock"
          }, void 0) : _jsx(Icon, {
            className: prefixCls + "-suffix-icon",
            icon: "time"
          }, void 0),
          clearIcon: _jsx(Icon, {
            className: prefixCls + "-clear-icon",
            icon: "close"
          }, void 0),
          allowClear: true
        }, restProps, {
          className: classNames(className, (_a = {}, _a[prefixCls + "-" + type] = type, _a[prefixCls + "-" + size] = size, _a[prefixCls + "-borderless"] = !bordered, _a))
        }, additionalOverrideProps, {
          locale: locale.lang,
          prefixCls: prefixCls,
          generateConfig: generateConfig,
          prevIcon: _jsx(Icon, {
            className: prefixCls + "-prev-icon",
            icon: "chevron-left-2"
          }, void 0),
          nextIcon: _jsx(Icon, {
            className: prefixCls + "-next-icon",
            icon: "chevron-right-2"
          }, void 0),
          superPrevIcon: _jsx(Icon, {
            className: prefixCls + "-super-prev-icon",
            icon: "fold-left"
          }, void 0),
          superNextIcon: _jsx(Icon, {
            className: prefixCls + "-super-next-icon",
            icon: "fold-right"
          }, void 0),
          components: PickerComponents
        }), void 0);
      };

      _this.state = {
        timeFormat: (_a = props.initialTimeFormat) !== null && _a !== void 0 ? _a : TimeFormat.hms
      };
      return _this;
    }

    RangePicker.prototype.render = function () {
      var local = this.getDefaultLocale();
      return this.renderPicker(local);
    };

    RangePicker.contextType = BaseContext;
    return RangePicker;
  }(React.Component);

  return RangePicker;
}

