import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { Alert, DatePicker, Button, Label, Sheet, Tooltip } from '@bigbinary/neeto-atoms';
import classnames from 'classnames';
import { dynamicArray, noop, isPresent } from '@bigbinary/neeto-cist';
import { withT } from '@bigbinary/neeto-commons-frontend/v2/react-utils';
import { dayjs, dateFormat } from '@bigbinary/neeto-commons-frontend/v2/utils';
import { useTranslation, Trans } from 'react-i18next';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { useState, useEffect } from 'react';
import Profile from '@bigbinary/neeto-team-members-frontend/Profile';
import { globalProps as globalProps$1 } from '@bigbinary/neeto-commons-frontend/v2/initializers';
import { c as createLucideIcon } from '../createLucideIcon-mNMhCCpf.js';

/**
 * @license lucide-react v1.7.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */


const __iconNode$2 = [
  ["circle", { cx: "12", cy: "12", r: "10", key: "1mglay" }],
  ["path", { d: "M12 6v6l4 2", key: "mmk7yg" }]
];
const Clock = createLucideIcon("clock", __iconNode$2);

/**
 * @license lucide-react v1.7.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */


const __iconNode$1 = [
  ["path", { d: "M15 3h6v6", key: "1q9fwt" }],
  ["path", { d: "M10 14 21 3", key: "gplh6r" }],
  ["path", { d: "M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6", key: "a6xqqp" }]
];
const ExternalLink = createLucideIcon("external-link", __iconNode$1);

/**
 * @license lucide-react v1.7.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */


const __iconNode = [
  ["path", { d: "M10 11v6", key: "nco0om" }],
  ["path", { d: "M14 11v6", key: "outv1u" }],
  ["path", { d: "M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6", key: "miytrc" }],
  ["path", { d: "M3 6h18", key: "d0wm0j" }],
  ["path", { d: "M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2", key: "e791ji" }]
];
const Trash2 = createLucideIcon("trash-2", __iconNode);

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-testid": "reset-alert",
    submitButtonLabel: t("neetoMolecules.common.actions.reset")
  }, props));
});

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-testid": "publish-later-datepicker-input-field",
      dateFormat: DATE_FORMAT,
      label: t("neetoMolecules.publishBlock.dateTime"),
      timeFormat: TIME_FORMAT,
      value: datetimeValue,
      timePickerProps: {
        "data-testid": "publish-later-timepicker-input-field",
        clearIcon: false,
        disabledTime: disabledTime
      },
      onChange: function onChange(date) {
        setDatetimeValue(date.startOf("hour"));
      }
    }), /*#__PURE__*/jsx("span", {
      className: "text-muted-foreground text-xs",
      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: "sm",
            variant: "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",
    children: [/*#__PURE__*/jsx(Label, {
      className: "mb-2",
      children: t("neetoMolecules.publishBlock.preview")
    }), /*#__PURE__*/jsx("div", {
      className: "bg-muted w-full rounded p-4",
      children: /*#__PURE__*/jsxs("div", {
        className: "flex flex-col items-start gap-3",
        children: [coverImageUrl && /*#__PURE__*/jsx("div", {
          className: "w-full flex-shrink-0",
          children: /*#__PURE__*/jsx("img", {
            alt: title,
            className: "h-52 w-full rounded object-cover",
            src: coverImageUrl
          })
        }), /*#__PURE__*/jsxs("div", {
          className: "flex w-full min-w-0 flex-col items-start gap-1",
          children: [/*#__PURE__*/jsx("h4", {
            className: "text-lg font-medium break-words",
            children: title
          }), publicUrl && /*#__PURE__*/jsx("a", {
            className: "text-primary 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(Sheet, {
    isOpen: isOpen,
    onClose: onClose,
    children: [/*#__PURE__*/jsx(Sheet.Header, {
      children: /*#__PURE__*/jsx(Sheet.Title, {
        className: "text-2xl leading-normal font-semibold",
        "data-testid": "publish-pane-heading",
        children: t("neetoMolecules.publishBlock.publishLater")
      })
    }), /*#__PURE__*/jsx(Sheet.Body, {
      hasFooter: true,
      children: /*#__PURE__*/jsxs("div", {
        className: "flex w-full flex-col gap-y-6 pb-6",
        children: [/*#__PURE__*/jsx(Form, {
          datetimeValue: datetimeValue,
          onClose: onClose,
          setDatetimeValue: setDatetimeValue
        }), previewProps && /*#__PURE__*/jsx(PublishPreview, _objectSpread$1({}, previewProps))]
      })
    }), /*#__PURE__*/jsxs(Sheet.Footer, {
      className: "flex items-center gap-x-2",
      children: [/*#__PURE__*/jsx(Button, {
        "data-testid": "publish-submit-button",
        disabled: isSubmitting,
        loading: isSubmitting,
        onClick: function onClick() {
          return onPublish({
            datetimeValue: datetimeValue
          });
        },
        children: t("neetoMolecules.publishBlock.publishLater")
      }), /*#__PURE__*/jsx(Button, {
        "data-testid": "publish-cancel-button",
        variant: "ghost",
        onClick: onClose,
        children: t("neetoMolecules.common.actions.cancel")
      })]
    })]
  });
});

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 = ["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 ViewDraftButton = withT(function (_ref) {
  var t = _ref.t,
    props = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/jsx(Tooltip, {
    content: t("neetoMolecules.publishBlock.viewDraftVersion"),
    position: "top",
    children: /*#__PURE__*/jsx("span", {
      className: "inline-flex",
      children: /*#__PURE__*/jsx(Button, _objectSpread({
        "data-testid": "view-draft-button",
        icon: ExternalLink,
        target: "_blank",
        variant: "secondary"
      }, props))
    })
  });
});
var ResetDraftButton = withT(function (_ref2) {
  var t = _ref2.t,
    props = _objectWithoutProperties(_ref2, _excluded2);
  return /*#__PURE__*/jsx(Tooltip, {
    content: t("neetoMolecules.publishBlock.deleteDraftVersion"),
    position: "top",
    children: /*#__PURE__*/jsx("span", {
      className: "inline-flex",
      children: /*#__PURE__*/jsx(Button, _objectSpread({
        "data-testid": "draft-button",
        icon: Trash2,
        variant: "secondary"
      }, props))
    })
  });
});
var PublishButton = function PublishButton(_ref3) {
  var 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);
  var _useTranslation = useTranslation(),
    t = _useTranslation.t;
  var isTooltipDisabled = !tooltipProps || !showTooltipWhenDisabled && disabled;
  return /*#__PURE__*/jsx(Tooltip, {
    content: tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.content,
    disabled: isTooltipDisabled,
    position: tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.position,
    children: /*#__PURE__*/jsx("div", {
      className: "inline-flex",
      children: /*#__PURE__*/jsx(Button, _objectSpread(_objectSpread({
        disabled: disabled,
        "data-testid": "publish-button"
      }, props), {}, {
        children: t("neetoMolecules.common.actions.publish")
      }))
    })
  });
};
var PublishPreviewButton = withT(function (_ref4) {
  var t = _ref4.t,
    props = _objectWithoutProperties(_ref4, _excluded4);
  return /*#__PURE__*/jsx(Tooltip, {
    content: t("neetoMolecules.publishBlock.viewPublishedVersion"),
    position: "top",
    children: /*#__PURE__*/jsx("span", {
      className: "inline-flex",
      children: /*#__PURE__*/jsx(Button, _objectSpread({
        "data-testid": "publish-preview-button",
        icon: ExternalLink,
        target: "_blank",
        className: classnames({
          "pointer-events-none": props.disabled
        }, props.className)
      }, props))
    })
  });
});
var PublishBlock = function PublishBlock(_ref5) {
  var renderDraftButtons = _ref5.renderDraftButtons,
    renderPublishButtons = _ref5.renderPublishButtons,
    datetime = _ref5.datetime;
  var _useTranslation2 = useTranslation(),
    t = _useTranslation2.t;
  var draftBlockButtons = renderDraftButtons({
    ViewDraftButton: ViewDraftButton,
    ResetDraftButton: ResetDraftButton
  });
  return /*#__PURE__*/jsxs("div", {
    className: "neeto-molecules-publish-block flex items-center justify-end gap-3",
    "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, {
          className: "size-4"
        })
      })
    }), draftBlockButtons && /*#__PURE__*/jsxs(Fragment, {
      children: [/*#__PURE__*/jsxs("div", {
        className: "flex items-center gap-2",
        children: [/*#__PURE__*/jsx("span", {
          className: "text-muted-foreground text-sm leading-normal",
          "data-testid": "publish-block-draft-label",
          children: t("neetoMolecules.common.actions.draft")
        }), /*#__PURE__*/jsx("div", {
          className: "flex items-center gap-1",
          children: draftBlockButtons
        })]
      }), /*#__PURE__*/jsx("div", {
        className: "bg-border h-6 w-px"
      })]
    }), /*#__PURE__*/jsx("div", {
      className: "neeto-molecules-publish-block__action-btn inline-flex flex-row items-center justify-center gap-px",
      children: renderPublishButtons({
        PublishButton: PublishButton,
        PublishPreviewButton: PublishPreviewButton
      })
    })]
  });
};
PublishBlock.Alert = AlertBlock;
PublishBlock.SchedulePane = PublishSchedulePane;

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