'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var classnames = require('classnames');
var neetoCist = require('@bigbinary/neeto-cist');
var withT = require('@bigbinary/neeto-commons-frontend/react-utils/withT');
var utils = require('@bigbinary/neeto-commons-frontend/utils');
var ExternalLink = require('@bigbinary/neeto-icons/ExternalLink');
var Delete = require('@bigbinary/neeto-icons/Delete');
var Clock = require('@bigbinary/neeto-icons/Clock');
var Button = require('@bigbinary/neetoui/Button');
var Tooltip = require('@bigbinary/neetoui/Tooltip');
var Typography = require('@bigbinary/neetoui/Typography');
var ramda = require('ramda');
var reactI18next = require('react-i18next');
var reactUtils = require('@bigbinary/neeto-commons-frontend/react-utils');
var Alert = require('@bigbinary/neetoui/Alert');
var jsxRuntime = require('react/jsx-runtime');
var injectCss = require('./inject-css-B6qYtOJe.js');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var React = require('react');
var Pane = require('@bigbinary/neetoui/Pane');
var Profile = require('@bigbinary/neeto-team-members-frontend/Profile');
var DatePicker = require('@bigbinary/neetoui/DatePicker');
var initializers = require('@bigbinary/neeto-commons-frontend/initializers');

var _excluded$1 = ["t"];
function ownKeys$2(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var AlertBlock = reactUtils.withT(function (_ref) {
  var t = _ref.t,
    props = _objectWithoutProperties(_ref, _excluded$1);
  return /*#__PURE__*/jsxRuntime.jsx(Alert, _objectSpread$2({
    "data-cy": "reset-alert",
    "data-testid": "reset-alert",
    submitButtonLabel: t("neetoMolecules.common.actions.reset")
  }, props));
});

var css = ".neeto-molecules-publish-block{align-items:center;display:flex;gap:.75rem;justify-content:flex-end}.neeto-molecules-publish-block__action-btn{align-items:center;display:inline-flex;flex-direction:row;gap:.0625rem;justify-content:center}.neeto-molecules-publish-block__action-btn>div:first-child>.neeto-ui-btn{border-end-end-radius:0;border-start-end-radius:0}.neeto-molecules-publish-block__action-btn>.neeto-ui-btn:last-child{border-end-start-radius:0;border-start-start-radius:0}";
injectCss.n(css,{});

var _globalProps$user;
var TIME_FORMAT = "h A";
var DATE_FORMAT = ((_globalProps$user = initializers.globalProps.user) === null || _globalProps$user === void 0 ? void 0 : _globalProps$user.dateFormat) || "DD/MM/YYYY";

var disabledDate = function disabledDate(current) {
  return current && current < utils.dayjs().startOf("day");
};
var disabledTime = function disabledTime(selectedDate) {
  var current = utils.dayjs(selectedDate).startOf("day");
  var today = utils.dayjs().startOf("day");
  if (current.isBefore(today)) {
    return {
      disabledHours: function disabledHours() {
        return neetoCist.dynamicArray(24, function (i) {
          return i;
        });
      }
    };
  }
  if (current.isSame(today)) {
    var currentTime = utils.dayjs();
    return {
      disabledHours: function disabledHours() {
        return neetoCist.dynamicArray(currentTime.hour() + 1, function (i) {
          return i;
        });
      }
    };
  }
  return {};
};

var Form = function Form(_ref) {
  var _globalProps$user$tim, _globalProps$user;
  var datetimeValue = _ref.datetimeValue,
    setDatetimeValue = _ref.setDatetimeValue,
    onClose = _ref.onClose;
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var _Profile$usePaneState = Profile.usePaneState(),
    _Profile$usePaneState2 = _slicedToArray(_Profile$usePaneState, 2);
    _Profile$usePaneState2[0];
    var setIsProfilePaneOpen = _Profile$usePaneState2[1];
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: "flex flex-col gap-y-1",
    children: [/*#__PURE__*/jsxRuntime.jsx(DatePicker, {
      disabledDate: disabledDate,
      required: true,
      showTime: true,
      allowClear: false,
      "data-cy": "publish-later-datepicker-input-field",
      "data-testid": "publish-later-datepicker-input-field",
      dateFormat: DATE_FORMAT,
      label: t("neetoMolecules.publishBlock.dateTime"),
      timeFormat: TIME_FORMAT,
      value: datetimeValue,
      timePickerProps: {
        "data-cy": "publish-later-timepicker-input-field",
        "data-testid": "publish-later-timepicker-input-field",
        clearIcon: false,
        disabledTime: disabledTime
      },
      onChange: function onChange(date) {
        setDatetimeValue(date.startOf("hour"));
      }
    }), /*#__PURE__*/jsxRuntime.jsx(Typography, {
      style: "body3",
      children: /*#__PURE__*/jsxRuntime.jsx(reactI18next.Trans, {
        i18nKey: "neetoMolecules.publishBlock.timezoneDescription",
        values: {
          timezone: (_globalProps$user$tim = (_globalProps$user = globalProps.user) === null || _globalProps$user === void 0 ? void 0 : _globalProps$user.timeZone) !== null && _globalProps$user$tim !== void 0 ? _globalProps$user$tim : utils.dayjs.tz.guess()
        },
        components: {
          button: /*#__PURE__*/jsxRuntime.jsx(Button, {
            size: "small",
            style: "link",
            onClick: function onClick() {
              onClose();
              setIsProfilePaneOpen(true);
            }
          })
        }
      })
    })]
  });
};

var PublishPreview = function PublishPreview(_ref) {
  var coverImageUrl = _ref.coverImageUrl,
    title = _ref.title,
    publicUrl = _ref.publicUrl;
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  if (!title) return null;
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: "flex flex-col gap-y-1",
    children: [/*#__PURE__*/jsxRuntime.jsx(Typography, {
      className: "neeto-ui-label mb-2",
      style: "body2",
      children: t("neetoMolecules.publishBlock.preview")
    }), /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "neeto-ui-rounded neeto-ui-bg-gray-100 w-full p-4",
      children: /*#__PURE__*/jsxRuntime.jsxs("div", {
        className: "flex flex-col items-start gap-4",
        children: [coverImageUrl && /*#__PURE__*/jsxRuntime.jsx("div", {
          className: "flex-shrink-0",
          children: /*#__PURE__*/jsxRuntime.jsx("img", {
            alt: title,
            className: "neeto-ui-rounded h-24 w-40 object-cover",
            src: coverImageUrl
          })
        }), /*#__PURE__*/jsxRuntime.jsxs("div", {
          className: "flex w-full min-w-0 flex-col items-start gap-4",
          children: [/*#__PURE__*/jsxRuntime.jsx(Typography, {
            className: "break-words",
            style: "h4",
            weight: "semibold",
            children: title
          }), publicUrl && /*#__PURE__*/jsxRuntime.jsx("a", {
            className: "neeto-ui-text-info-600 break-all",
            href: publicUrl,
            rel: "noopener noreferrer",
            target: "_blank",
            children: publicUrl
          })]
        })]
      })
    })]
  });
};

function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var PublishSettings = reactUtils.withT(function (_ref) {
  var t = _ref.t,
    isOpen = _ref.isOpen,
    onClose = _ref.onClose,
    _ref$isSubmitting = _ref.isSubmitting,
    isSubmitting = _ref$isSubmitting === void 0 ? false : _ref$isSubmitting,
    _ref$onPublish = _ref.onPublish,
    onPublish = _ref$onPublish === void 0 ? neetoCist.noop : _ref$onPublish,
    datetimeValue = _ref.datetimeValue,
    setDatetimeValue = _ref.setDatetimeValue,
    previewProps = _ref.previewProps;
  return /*#__PURE__*/jsxRuntime.jsxs(Pane, {
    isOpen: isOpen,
    onClose: onClose,
    children: [/*#__PURE__*/jsxRuntime.jsx(Pane.Header, {
      children: /*#__PURE__*/jsxRuntime.jsx(Typography, {
        "data-cy": "publish-pane-heading",
        "data-testid": "publish-pane-heading",
        lineHeight: "normal",
        style: "h2",
        weight: "semibold",
        children: t("neetoMolecules.publishBlock.publishLater")
      })
    }), /*#__PURE__*/jsxRuntime.jsx(Pane.Body, {
      children: /*#__PURE__*/jsxRuntime.jsxs("div", {
        className: "flex h-full w-full flex-col gap-y-8",
        children: [/*#__PURE__*/jsxRuntime.jsx(Form, {
          datetimeValue: datetimeValue,
          onClose: onClose,
          setDatetimeValue: setDatetimeValue
        }), previewProps && /*#__PURE__*/jsxRuntime.jsx(PublishPreview, _objectSpread$1({}, previewProps))]
      })
    }), /*#__PURE__*/jsxRuntime.jsxs(Pane.Footer, {
      className: "flex items-center gap-x-2",
      children: [/*#__PURE__*/jsxRuntime.jsx(Button, {
        "data-cy": "publish-submit-button",
        "data-testid": "publish-submit-button",
        disabled: isSubmitting,
        label: t("neetoMolecules.publishBlock.publishLater"),
        loading: isSubmitting,
        onClick: function onClick() {
          return onPublish({
            datetimeValue: datetimeValue
          });
        }
      }), /*#__PURE__*/jsxRuntime.jsx(Button, {
        "data-cy": "publish-cancel-button",
        "data-testid": "publish-cancel-button",
        label: t("neetoMolecules.common.actions.cancel"),
        style: "text",
        onClick: onClose
      })]
    })]
  });
});

var PublishSchedulePane = function PublishSchedulePane(_ref) {
  var onPublish = _ref.onPublish,
    onClose = _ref.onClose,
    isSubmitting = _ref.isSubmitting,
    datetime = _ref.datetime,
    isOpen = _ref.isOpen,
    previewProps = _ref.previewProps;
  var _useState = React.useState(datetime),
    _useState2 = _slicedToArray(_useState, 2),
    datetimeValue = _useState2[0],
    setDatetimeValue = _useState2[1];
  React.useEffect(function () {
    setDatetimeValue(datetime);
  }, [datetime]);
  return /*#__PURE__*/jsxRuntime.jsx(PublishSettings, {
    datetimeValue: datetimeValue,
    isOpen: isOpen,
    isSubmitting: isSubmitting,
    onClose: onClose,
    onPublish: onPublish,
    previewProps: previewProps,
    setDatetimeValue: setDatetimeValue
  });
};

var _excluded = ["t"],
  _excluded2 = ["t"],
  _excluded3 = ["t", "tooltipProps", "showTooltipWhenDisabled", "disabled"],
  _excluded4 = ["t"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var unUsedI18nProps = ["tReady", "i18n"];
var ViewDraftButton = withT(function (_ref) {
  var t = _ref.t,
    props = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/jsxRuntime.jsx(Button, _objectSpread({
    "data-cy": "view-draft-button",
    "data-testid": "view-draft-button",
    icon: ExternalLink,
    style: "secondary",
    target: "_blank",
    tooltipProps: {
      content: t("neetoMolecules.publishBlock.viewDraftVersion"),
      position: "top",
      touch: ["hold", 500]
    }
  }, ramda.omit(unUsedI18nProps, props)));
});
var ResetDraftButton = withT(function (_ref2) {
  var t = _ref2.t,
    props = _objectWithoutProperties(_ref2, _excluded2);
  return /*#__PURE__*/jsxRuntime.jsx(Button, _objectSpread({
    "data-cy": "draft-button",
    "data-testid": "draft-button",
    icon: Delete,
    style: "secondary",
    tooltipProps: {
      content: t("neetoMolecules.publishBlock.deleteDraftVersion"),
      position: "top",
      touch: ["hold", 500]
    }
  }, ramda.omit(unUsedI18nProps, props)));
});
var PublishButton = withT(function (_ref3) {
  var t = _ref3.t,
    tooltipProps = _ref3.tooltipProps,
    _ref3$showTooltipWhen = _ref3.showTooltipWhenDisabled,
    showTooltipWhenDisabled = _ref3$showTooltipWhen === void 0 ? false : _ref3$showTooltipWhen,
    _ref3$disabled = _ref3.disabled,
    disabled = _ref3$disabled === void 0 ? false : _ref3$disabled,
    props = _objectWithoutProperties(_ref3, _excluded3);
  return /*#__PURE__*/jsxRuntime.jsx(Tooltip, _objectSpread(_objectSpread({}, tooltipProps), {}, {
    disabled: !tooltipProps || !showTooltipWhenDisabled && disabled,
    children: /*#__PURE__*/jsxRuntime.jsx("div", {
      children: /*#__PURE__*/jsxRuntime.jsx(Button, _objectSpread({
        "data-cy": "publish-button",
        "data-testid": "publish-button",
        label: t("neetoMolecules.common.actions.publish")
      }, _objectSpread({
        disabled: disabled
      }, ramda.omit(unUsedI18nProps, props))))
    })
  }));
});
var PublishPreviewButton = withT(function (_ref4) {
  var t = _ref4.t,
    props = _objectWithoutProperties(_ref4, _excluded4);
  return /*#__PURE__*/jsxRuntime.jsx(Button, _objectSpread({
    "data-cy": "publish-preview-button",
    "data-testid": "publish-preview-button",
    icon: ExternalLink,
    target: "_blank",
    className: classnames({
      "pointer-events-none": props.disabled
    }, props.className),
    tooltipProps: {
      content: t("neetoMolecules.publishBlock.viewPublishedVersion"),
      position: "top",
      touch: ["hold", 500]
    }
  }, ramda.omit(unUsedI18nProps, props)));
});
var PublishBlock = function PublishBlock(_ref5) {
  var renderDraftButtons = _ref5.renderDraftButtons,
    renderPublishButtons = _ref5.renderPublishButtons,
    datetime = _ref5.datetime;
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var draftBlockButtons = renderDraftButtons({
    ViewDraftButton: ViewDraftButton,
    ResetDraftButton: ResetDraftButton
  });
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: "neeto-molecules-publish-block",
    "data-cy": "neeto-molecules-publish-block",
    "data-testid": "neeto-molecules-publish-block",
    children: [neetoCist.isPresent(datetime) && /*#__PURE__*/jsxRuntime.jsx(Tooltip, {
      position: "bottom",
      content: t("neetoMolecules.publishBlock.tooltip.publishLater", {
        date: utils.dateFormat.dateTime(datetime)
      }),
      children: /*#__PURE__*/jsxRuntime.jsx("div", {
        children: /*#__PURE__*/jsxRuntime.jsx(Clock, {})
      })
    }), draftBlockButtons && /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
      children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
        className: "flex items-center gap-2",
        children: [/*#__PURE__*/jsxRuntime.jsx(Typography, {
          className: "neeto-ui-text-gray-700",
          "data-cy": "publish-block-draft-label",
          lineHeight: "normal",
          style: "body2",
          children: t("neetoMolecules.common.actions.draft")
        }), /*#__PURE__*/jsxRuntime.jsx("div", {
          className: "flex items-center gap-1",
          children: draftBlockButtons
        })]
      }), /*#__PURE__*/jsxRuntime.jsx("div", {
        className: "neeto-ui-bg-gray-300 h-full w-px"
      })]
    }), /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "neeto-molecules-publish-block__action-btn",
      children: renderPublishButtons({
        PublishButton: PublishButton,
        PublishPreviewButton: PublishPreviewButton
      })
    })]
  });
};
PublishBlock.Alert = AlertBlock;
PublishBlock.SchedulePane = PublishSchedulePane;

module.exports = PublishBlock;
//# sourceMappingURL=PublishBlock.js.map
