"use strict";

var __extends = void 0 && (void 0).__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 = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__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 __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

var _a;

Object.defineProperty(exports, "__esModule", {
  value: true
});

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var classnames_1 = __importDefault(require("classnames"));

var rc_picker_1 = require("rc-picker");

var context_1 = require("../../style/context");

var dayjs_1 = __importDefault(require("dayjs"));

require("dayjs/locale/zh-cn");

require("dayjs/locale/en");

var en_US_1 = __importDefault(require("../languages/en-US"));

var zh_CN_1 = __importDefault(require("../languages/zh-CN"));

var util_1 = require("../util");

var Icon_1 = __importDefault(require("../../icon/Icon"));

var RadioGroup_1 = __importDefault(require("../../radio/RadioGroup"));

var _1 = require("./");

var localeMap = (_a = {}, _a[context_1.LocaleEnum.en] = en_US_1["default"], _a[context_1.LocaleEnum.zh] = zh_CN_1["default"], _a);
var prefixCls = 'dmc-date-picker';

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 = 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 || context_1.LocaleEnum.zh; // eslint-disable-next-line @typescript-eslint/no-var-requires

        var local = localeMap[language];
        dayjs_1["default"].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 = [util_1.TimeFormat.hm, util_1.TimeFormat.hms].map(function (t) {
            return {
              value: t,
              label: util_1.getTimeFormatPlaceholder(t, local_1)
            };
          });
          return jsx_runtime_1.jsx(RadioGroup_1["default"], {
            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 === util_1.TimeFormat.hm) {
            showTime = true;
            format = 'HH:mm';
          } else if (timeFormat === util_1.TimeFormat.hms) {
            showTime = true;
            format = 'HH:mm:ss';
          }
        }

        var additionalOverrideProps = {};
        additionalOverrideProps = __assign(__assign(__assign(__assign({}, additionalOverrideProps), showTime ? _1.getTimeProps(__assign({
          format: format,
          picker: picker
        }, showTime)) : {}), picker === 'time' ? _1.getTimeProps(__assign(__assign({
          format: format
        }, _this.props), {
          picker: picker
        })) : {}), showTime ? {
          renderExtraFooter: _this._renderExtraFooter
        } : {});
        return jsx_runtime_1.jsx(rc_picker_1.RangePicker, __assign({
          separator: jsx_runtime_1.jsx("span", __assign({
            "aria-label": "to",
            className: prefixCls + "-separator"
          }, {
            children: jsx_runtime_1.jsx(Icon_1["default"], {
              className: prefixCls + "-separator-icon",
              icon: "remove"
            }, void 0)
          }), void 0),
          ref: _this.pickerRef,
          placeholder: util_1.getRangePlaceholder(picker, locale, placeholder),
          suffixIcon: picker === 'time' ? jsx_runtime_1.jsx(Icon_1["default"], {
            className: prefixCls + "-suffix-icon",
            icon: "time-clock"
          }, void 0) : jsx_runtime_1.jsx(Icon_1["default"], {
            className: prefixCls + "-suffix-icon",
            icon: "time"
          }, void 0),
          clearIcon: jsx_runtime_1.jsx(Icon_1["default"], {
            className: prefixCls + "-clear-icon",
            icon: "close"
          }, void 0),
          allowClear: true
        }, restProps, {
          className: classnames_1["default"](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_runtime_1.jsx(Icon_1["default"], {
            className: prefixCls + "-prev-icon",
            icon: "chevron-left-2"
          }, void 0),
          nextIcon: jsx_runtime_1.jsx(Icon_1["default"], {
            className: prefixCls + "-next-icon",
            icon: "chevron-right-2"
          }, void 0),
          superPrevIcon: jsx_runtime_1.jsx(Icon_1["default"], {
            className: prefixCls + "-super-prev-icon",
            icon: "fold-left"
          }, void 0),
          superNextIcon: jsx_runtime_1.jsx(Icon_1["default"], {
            className: prefixCls + "-super-next-icon",
            icon: "fold-right"
          }, void 0),
          components: _1.PickerComponents
        }), void 0);
      };

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

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

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

  return RangePicker;
}

exports["default"] = generateRangePicker;

