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 RCPicker from 'rc-picker';
import { getPlaceholder, getTimeFormatPlaceholder, prefixCls, TimeFormat, timeFormatValue } from "../util";
import { PickerComponents, getTimeProps } from "./";
import Icon from "../../icon/Icon";
import RadioGroup from "../../radio/RadioGroup";
import "../style/index.css";
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";
var localeMap = (_a = {}, _a[LocaleEnum.en] = en, _a[LocaleEnum.zh] = zh, _a);
export default function generatePicker(generateConfig) {
  function getPicker(picker, displayName) {
    var Picker =
    /** @class */
    function (_super) {
      __extends(Picker, _super);

      function Picker(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.handleRadioChange = function (val) {
          var _a, _b;

          _this.setState({
            timeFormat: val,
            timeFormatted: true
          });

          (_b = (_a = _this.props).onFormatChange) === null || _b === void 0 ? void 0 : _b.call(_a, val);
        };

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

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

          var renderExtraFooter = _this.props.renderExtraFooter;

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

        _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.renderPicker = function (locale) {
          var _a, _b;

          var timeFormat = _this.state.timeFormat;

          var _c = _this.props,
              className = _c.className,
              _d = _c.bordered,
              bordered = _d === void 0 ? true : _d,
              placeholder = _c.placeholder,
              dropdownClassName = _c.dropdownClassName,
              type = _c.type,
              size = _c.size,
              onChange = _c.onChange,
              onSelect = _c.onSelect,
              format = _c.format,
              restProps = __rest(_c, ["className", "bordered", "placeholder", "dropdownClassName", "type", "size", "onChange", "onSelect", "format"]);

          var showTime = _this.props.showTime;
          var newFormat = format;

          if (!showTime) {
            if (timeFormat === TimeFormat.no) {
              showTime = false;
            } else if (timeFormat === TimeFormat.h) {
              showTime = true;
              newFormat = 'HH';
            } else if (timeFormat === TimeFormat.hm) {
              showTime = true;
              newFormat = 'HH:mm';
            } else if (timeFormat === TimeFormat.hms) {
              showTime = true;
              newFormat = 'HH:mm:ss';
            }
          }

          var additionalProps = {
            showToday: true
          };
          var additionalOverrideProps = {};

          if (picker) {
            additionalOverrideProps.picker = picker;
          }

          var mergedPicker = picker || _this.props.picker;
          additionalOverrideProps = __assign(__assign(__assign({}, additionalOverrideProps), showTime ? getTimeProps(__assign({
            format: newFormat,
            picker: mergedPicker
          }, showTime)) : {}), mergedPicker === 'time' ? getTimeProps(__assign(__assign({
            format: newFormat
          }, _this.props), {
            picker: mergedPicker
          })) : {});

          var _onChange = function _onChange(value, dateString) {
            onChange && onChange(value, dateString, timeFormat);

            _this.setState({
              value: value,
              dateString: dateString
            });
          };

          var _onSelect = function _onSelect(value) {
            onSelect && onSelect(value, timeFormat);

            if (value !== _this.state.value && timeFormat !== TimeFormat.no) {
              _this.setState({
                selected: true
              });
            }
          };

          var _pickerOnchange = function _pickerOnchange() {
            var _a = _this.state,
                value = _a.value,
                timeFormatted = _a.timeFormatted,
                selected = _a.selected;
            var _b = _this.props,
                propsValue = _b.value,
                format = _b.format;

            if (timeFormatted && !selected) {
              _onChange(value || propsValue, dayjs(value).format(format || timeFormatValue(timeFormat)));
            }

            _this.setState({
              timeFormatted: false,
              selected: false
            });
          };

          return _jsx(RCPicker, __assign({
            ref: _this.pickerRef,
            placeholder: getPlaceholder(mergedPicker, locale, placeholder),
            suffixIcon: mergedPicker === '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
          }, additionalProps, restProps, additionalOverrideProps, {
            locale: locale.lang,
            className: classNames((_a = {}, _a[prefixCls + "-" + type] = type, _a[prefixCls + "-" + size] = size, _a[prefixCls + "-borderless"] = !bordered, _a), className),
            dropdownClassName: classNames(dropdownClassName, (_b = {}, _b[prefixCls + "-show-time"] = showTime, _b)),
            prefixCls: prefixCls,
            onChange: _onChange,
            onSelect: _onSelect,
            format: format || timeFormatValue(timeFormat),
            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({
              onClick: _pickerOnchange
            }),
            renderExtraFooter: _this._renderExtraFooter
          }), void 0);
        };

        _this.state = {
          panelMode: picker,
          timeFormat: (_a = props.initialTimeFormat) !== null && _a !== void 0 ? _a : TimeFormat.no,
          selected: false,
          timeFormatted: false
        };
        return _this;
      }

      Picker.getDerivedStateFromProps = function (nextProps, prevState) {
        if (nextProps.timeFormat && nextProps.timeFormat !== prevState.timeFormat) {
          return {
            timeFormat: nextProps.timeFormat
          };
        }

        return prevState;
      };

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

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

    if (displayName) {
      Picker.displayName = displayName;
    }

    return Picker;
  }

  var DatePicker = getPicker();
  var WeekPicker = getPicker('week', 'WeekPicker');
  var MonthPicker = getPicker('month', 'MonthPicker');
  var YearPicker = getPicker('year', 'YearPicker');
  var TimePicker = getPicker('time', 'TimePicker');
  return {
    DatePicker: DatePicker,
    WeekPicker: WeekPicker,
    MonthPicker: MonthPicker,
    YearPicker: YearPicker,
    TimePicker: TimePicker
  };
}

