import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import classnames from 'classnames';
import { dynamicArray, noop, isPresent } from '@bigbinary/neeto-cist';
import withT$1 from '@bigbinary/neeto-commons-frontend/react-utils/withT';
import { dayjs, dateFormat } from '@bigbinary/neeto-commons-frontend/utils';
import ExternalLink from '@bigbinary/neeto-icons/ExternalLink';
import Delete from '@bigbinary/neeto-icons/Delete';
import Clock from '@bigbinary/neeto-icons/Clock';
import Button from '@bigbinary/neetoui/Button';
import Tooltip from '@bigbinary/neetoui/Tooltip';
import Typography from '@bigbinary/neetoui/Typography';
import { omit } from 'ramda';
import { useTranslation, Trans } from 'react-i18next';
import { withT } from '@bigbinary/neeto-commons-frontend/react-utils';
import Alert from '@bigbinary/neetoui/Alert';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { n } from './inject-css-C2dztUxs.js';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { useState, useEffect } from 'react';
import Pane from '@bigbinary/neetoui/Pane';
import Profile from '@bigbinary/neeto-team-members-frontend/Profile';
import DatePicker from '@bigbinary/neetoui/DatePicker';
import { globalProps as globalProps$1 } from '@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 = withT(function (_ref) {
  var t = _ref.t,
    props = _objectWithoutProperties(_ref, _excluded$1);
  return /*#__PURE__*/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}";
n(css,{});

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

var disabledDate = function disabledDate(current) {
  return current && current < dayjs().startOf("day");
};
var disabledTime = function disabledTime(selectedDate) {
  var current = dayjs(selectedDate).startOf("day");
  var today = dayjs().startOf("day");
  if (current.isBefore(today)) {
    return {
      disabledHours: function disabledHours() {
        return dynamicArray(24, function (i) {
          return i;
        });
      }
    };
  }
  if (current.isSame(today)) {
    var currentTime = dayjs();
    return {
      disabledHours: function disabledHours() {
        return 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 = 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__*/jsxs("div", {
    className: "flex flex-col gap-y-1",
    children: [/*#__PURE__*/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__*/jsx(Typography, {
      style: "body3",
      children: /*#__PURE__*/jsx(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 : dayjs.tz.guess()
        },
        components: {
          button: /*#__PURE__*/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 = useTranslation(),
    t = _useTranslation.t;
  if (!title) return null;
  return /*#__PURE__*/jsxs("div", {
    className: "flex flex-col gap-y-1",
    children: [/*#__PURE__*/jsx(Typography, {
      className: "neeto-ui-label mb-2",
      style: "body2",
      children: t("neetoMolecules.publishBlock.preview")
    }), /*#__PURE__*/jsx("div", {
      className: "neeto-ui-rounded neeto-ui-bg-gray-100 w-full p-4",
      children: /*#__PURE__*/jsxs("div", {
        className: "flex flex-col items-start gap-4",
        children: [coverImageUrl && /*#__PURE__*/jsx("div", {
          className: "flex-shrink-0",
          children: /*#__PURE__*/jsx("img", {
            alt: title,
            className: "neeto-ui-rounded h-24 w-40 object-cover",
            src: coverImageUrl
          })
        }), /*#__PURE__*/jsxs("div", {
          className: "flex w-full min-w-0 flex-col items-start gap-4",
          children: [/*#__PURE__*/jsx(Typography, {
            className: "break-words",
            style: "h4",
            weight: "semibold",
            children: title
          }), publicUrl && /*#__PURE__*/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 = 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 ? noop : _ref$onPublish,
    datetimeValue = _ref.datetimeValue,
    setDatetimeValue = _ref.setDatetimeValue,
    previewProps = _ref.previewProps;
  return /*#__PURE__*/jsxs(Pane, {
    isOpen: isOpen,
    onClose: onClose,
    children: [/*#__PURE__*/jsx(Pane.Header, {
      children: /*#__PURE__*/jsx(Typography, {
        "data-cy": "publish-pane-heading",
        "data-testid": "publish-pane-heading",
        lineHeight: "normal",
        style: "h2",
        weight: "semibold",
        children: t("neetoMolecules.publishBlock.publishLater")
      })
    }), /*#__PURE__*/jsx(Pane.Body, {
      children: /*#__PURE__*/jsxs("div", {
        className: "flex h-full w-full flex-col gap-y-8",
        children: [/*#__PURE__*/jsx(Form, {
          datetimeValue: datetimeValue,
          onClose: onClose,
          setDatetimeValue: setDatetimeValue
        }), previewProps && /*#__PURE__*/jsx(PublishPreview, _objectSpread$1({}, previewProps))]
      })
    }), /*#__PURE__*/jsxs(Pane.Footer, {
      className: "flex items-center gap-x-2",
      children: [/*#__PURE__*/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__*/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 = useState(datetime),
    _useState2 = _slicedToArray(_useState, 2),
    datetimeValue = _useState2[0],
    setDatetimeValue = _useState2[1];
  useEffect(function () {
    setDatetimeValue(datetime);
  }, [datetime]);
  return /*#__PURE__*/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$1(function (_ref) {
  var t = _ref.t,
    props = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/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]
    }
  }, omit(unUsedI18nProps, props)));
});
var ResetDraftButton = withT$1(function (_ref2) {
  var t = _ref2.t,
    props = _objectWithoutProperties(_ref2, _excluded2);
  return /*#__PURE__*/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]
    }
  }, omit(unUsedI18nProps, props)));
});
var PublishButton = withT$1(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__*/jsx(Tooltip, _objectSpread(_objectSpread({}, tooltipProps), {}, {
    disabled: !tooltipProps || !showTooltipWhenDisabled && disabled,
    children: /*#__PURE__*/jsx("div", {
      children: /*#__PURE__*/jsx(Button, _objectSpread({
        "data-cy": "publish-button",
        "data-testid": "publish-button",
        label: t("neetoMolecules.common.actions.publish")
      }, _objectSpread({
        disabled: disabled
      }, omit(unUsedI18nProps, props))))
    })
  }));
});
var PublishPreviewButton = withT$1(function (_ref4) {
  var t = _ref4.t,
    props = _objectWithoutProperties(_ref4, _excluded4);
  return /*#__PURE__*/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]
    }
  }, omit(unUsedI18nProps, props)));
});
var PublishBlock = function PublishBlock(_ref5) {
  var renderDraftButtons = _ref5.renderDraftButtons,
    renderPublishButtons = _ref5.renderPublishButtons,
    datetime = _ref5.datetime;
  var _useTranslation = useTranslation(),
    t = _useTranslation.t;
  var draftBlockButtons = renderDraftButtons({
    ViewDraftButton: ViewDraftButton,
    ResetDraftButton: ResetDraftButton
  });
  return /*#__PURE__*/jsxs("div", {
    className: "neeto-molecules-publish-block",
    "data-cy": "neeto-molecules-publish-block",
    "data-testid": "neeto-molecules-publish-block",
    children: [isPresent(datetime) && /*#__PURE__*/jsx(Tooltip, {
      position: "bottom",
      content: t("neetoMolecules.publishBlock.tooltip.publishLater", {
        date: dateFormat.dateTime(datetime)
      }),
      children: /*#__PURE__*/jsx("div", {
        children: /*#__PURE__*/jsx(Clock, {})
      })
    }), draftBlockButtons && /*#__PURE__*/jsxs(Fragment, {
      children: [/*#__PURE__*/jsxs("div", {
        className: "flex items-center gap-2",
        children: [/*#__PURE__*/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__*/jsx("div", {
          className: "flex items-center gap-1",
          children: draftBlockButtons
        })]
      }), /*#__PURE__*/jsx("div", {
        className: "neeto-ui-bg-gray-300 h-full w-px"
      })]
    }), /*#__PURE__*/jsx("div", {
      className: "neeto-molecules-publish-block__action-btn",
      children: renderPublishButtons({
        PublishButton: PublishButton,
        PublishPreviewButton: PublishPreviewButton
      })
    })]
  });
};
PublishBlock.Alert = AlertBlock;
PublishBlock.SchedulePane = PublishSchedulePane;

export { PublishBlock as default };
//# sourceMappingURL=PublishBlock.js.map
