"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.HypothesisTitle = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _lodash = require("lodash");

var _reactHookForm = require("react-hook-form");

var _Avatar = require("../Avatar");

var _EditableText = require("../EditableText");

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var HypothesisTitle = function HypothesisTitle(_ref) {
  var _node$title$trim, _node$title, _node$created_by;

  var node = _ref.node,
      api = _ref.api;
  var handleApiUpdate = (0, _react.useRef)((0, _lodash.debounce)(function (params) {
    if (api !== null && api !== void 0 && api.updateMutation && node !== null && node !== void 0 && node.id) {
      api.updateMutation.mutate({
        id: node.id,
        params: params
      });
    }
  }, 1000)).current;

  var _useForm = (0, _reactHookForm.useForm)({
    defaultValues: {
      title: (_node$title$trim = (_node$title = node.title) === null || _node$title === void 0 ? void 0 : _node$title.trim()) !== null && _node$title$trim !== void 0 ? _node$title$trim : ""
    }
  }),
      watch = _useForm.watch,
      setValue = _useForm.setValue,
      getValues = _useForm.getValues;

  watch();
  (0, _react.useEffect)(function () {
    var subscription = watch(function (value) {
      handleApiUpdate(value);
    });
    return function () {
      return subscription.unsubscribe();
    };
  }, [watch, getValues, handleApiUpdate]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "flex",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Avatar.Avatar, {
        size: "sm",
        user: node === null || node === void 0 ? void 0 : node.created_by
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        className: "font-inter text-dark-gray font-normal text-sm ml-1.5 mt-0.5",
        children: node === null || node === void 0 ? void 0 : (_node$created_by = node.created_by) === null || _node$created_by === void 0 ? void 0 : _node$created_by.name
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "mt-2 mr-2 mb-2 pr-8 flex flex-nowrap",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_EditableText.EditableText, {
        value: getValues("title"),
        sharedClassName: "text-2xl font-semibold text-gray-900 grow",
        onChange: function onChange(value) {
          setValue("title", value);
        }
      })
    })]
  });
};

exports.HypothesisTitle = HypothesisTitle;
HypothesisTitle.propTypes = {
  node: _propTypes.default.shape({
    id: _propTypes.default.string,
    title: _propTypes.default.string,
    created_by: _propTypes.default.shape({
      name: _propTypes.default.string,
      avatar: _propTypes.default.string
    })
  }).isRequired,
  api: _propTypes.default.shape({}).isRequired
};