"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 = __importDefault(require("rc-picker"));

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

var _1 = require("./");

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

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

require("../style/index.css");

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 localeMap = (_a = {}, _a[context_1.LocaleEnum.en] = en_US_1["default"], _a[context_1.LocaleEnum.zh] = zh_CN_1["default"], _a);

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 = 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 = [util_1.TimeFormat.no, 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: timeFormat,
              options: options,
              className: util_1.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 || 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.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 === util_1.TimeFormat.no) {
              showTime = false;
            } else if (timeFormat === util_1.TimeFormat.h) {
              showTime = true;
              newFormat = 'HH';
            } else if (timeFormat === util_1.TimeFormat.hm) {
              showTime = true;
              newFormat = 'HH:mm';
            } else if (timeFormat === util_1.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 ? _1.getTimeProps(__assign({
            format: newFormat,
            picker: mergedPicker
          }, showTime)) : {}), mergedPicker === 'time' ? _1.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 !== util_1.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_1["default"](value).format(format || util_1.timeFormatValue(timeFormat)));
            }

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

          return jsx_runtime_1.jsx(rc_picker_1["default"], __assign({
            ref: _this.pickerRef,
            placeholder: util_1.getPlaceholder(mergedPicker, locale, placeholder),
            suffixIcon: mergedPicker === 'time' ? jsx_runtime_1.jsx(Icon_1["default"], {
              className: util_1.prefixCls + "-suffix-icon",
              icon: "time-clock"
            }, void 0) : jsx_runtime_1.jsx(Icon_1["default"], {
              className: util_1.prefixCls + "-suffix-icon",
              icon: "time"
            }, void 0),
            clearIcon: jsx_runtime_1.jsx(Icon_1["default"], {
              className: util_1.prefixCls + "-clear-icon",
              icon: "close"
            }, void 0),
            allowClear: true
          }, additionalProps, restProps, additionalOverrideProps, {
            locale: locale.lang,
            className: classnames_1["default"]((_a = {}, _a[util_1.prefixCls + "-" + type] = type, _a[util_1.prefixCls + "-" + size] = size, _a[util_1.prefixCls + "-borderless"] = !bordered, _a), className),
            dropdownClassName: classnames_1["default"](dropdownClassName, (_b = {}, _b[util_1.prefixCls + "-show-time"] = showTime, _b)),
            prefixCls: util_1.prefixCls,
            onChange: _onChange,
            onSelect: _onSelect,
            format: format || util_1.timeFormatValue(timeFormat),
            generateConfig: generateConfig,
            prevIcon: jsx_runtime_1.jsx(Icon_1["default"], {
              className: util_1.prefixCls + "-prev-icon",
              icon: "chevron-left-2"
            }, void 0),
            nextIcon: jsx_runtime_1.jsx(Icon_1["default"], {
              className: util_1.prefixCls + "-next-icon",
              icon: "chevron-right-2"
            }, void 0),
            superPrevIcon: jsx_runtime_1.jsx(Icon_1["default"], {
              className: util_1.prefixCls + "-super-prev-icon",
              icon: "fold-left"
            }, void 0),
            superNextIcon: jsx_runtime_1.jsx(Icon_1["default"], {
              className: util_1.prefixCls + "-super-next-icon",
              icon: "fold-right"
            }, void 0),
            components: _1.PickerComponents({
              onClick: _pickerOnchange
            }),
            renderExtraFooter: _this._renderExtraFooter
          }), void 0);
        };

        _this.state = {
          panelMode: picker,
          timeFormat: (_a = props.initialTimeFormat) !== null && _a !== void 0 ? _a : util_1.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 = context_1.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
  };
}

exports["default"] = generatePicker;

