'use client';

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useModal } from '@faceless-ui/modal';
import { getTranslation } from '@payloadcms/translations';
import { formatAdminURL } from 'payload/shared';
import * as qs from 'qs-esm';
import React, { useCallback, useEffect, useState } from 'react';
import { toast } from 'sonner';
import { useForm } from '../../forms/Form/context.js';
import { useConfig } from '../../providers/Config/index.js';
import { useDocumentInfo } from '../../providers/DocumentInfo/index.js';
import { useLocale } from '../../providers/Locale/index.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { requests } from '../../utilities/api.js';
import { traverseForLocalizedFields } from '../../utilities/traverseForLocalizedFields.js';
import { ConfirmationModal } from '../ConfirmationModal/index.js';
import { PopupList } from '../Popup/index.js';
export function UnpublishButton({
  label: labelProp
} = {}) {
  const {
    id,
    collectionSlug,
    data: dataFromProps,
    globalSlug,
    hasPublishedDoc,
    hasPublishPermission,
    incrementVersionCount,
    isTrashed,
    setHasPublishedDoc,
    setMostRecentVersionIsAutosaved,
    setUnpublishedVersionCount
  } = useDocumentInfo();
  const {
    toggleModal
  } = useModal();
  const {
    config,
    getEntityConfig
  } = useConfig();
  const {
    reset: resetForm
  } = useForm();
  const {
    code: localeCode,
    label: localeLabel
  } = useLocale();
  const [unpublishAll, setUnpublishAll] = useState(false);
  const [hasLocalizedFields, setHasLocalizedFields] = useState(false);
  const unPublishModalSlug = `confirm-un-publish-${id}`;
  const {
    routes: {
      api
    },
    serverURL
  } = config;
  const {
    i18n,
    t
  } = useTranslation();
  const entityConfig = React.useMemo(() => {
    if (collectionSlug) {
      return getEntityConfig({
        collectionSlug
      });
    }
    if (globalSlug) {
      return getEntityConfig({
        globalSlug
      });
    }
  }, [collectionSlug, globalSlug, getEntityConfig]);
  const unpublish = useCallback(unpublishAll_0 => {
    void (async () => {
      let url;
      let method;
      const queryString = qs.stringify({
        depth: 0,
        'fallback-locale': 'null',
        locale: unpublishAll_0 ? undefined : localeCode,
        unpublishAllLocales: unpublishAll_0
      }, {
        addQueryPrefix: true
      });
      if (collectionSlug) {
        url = formatAdminURL({
          apiRoute: api,
          path: `/${collectionSlug}/${id}${queryString}`,
          serverURL
        });
        method = 'patch';
      }
      if (globalSlug) {
        url = formatAdminURL({
          apiRoute: api,
          path: `/globals/${globalSlug}${queryString}`,
          serverURL
        });
        method = 'post';
      }
      try {
        const res = await requests[method](url, {
          body: JSON.stringify({
            _status: 'draft'
          }),
          headers: {
            'Accept-Language': i18n.language,
            'Content-Type': 'application/json'
          }
        });
        if (res.status === 200) {
          void resetForm({
            ...(dataFromProps || {}),
            _status: 'draft'
          });
          toast.success(t('version:unpublishedSuccessfully'));
          incrementVersionCount();
          setUnpublishedVersionCount(1);
          setMostRecentVersionIsAutosaved(false);
          setHasPublishedDoc(false);
        } else {
          try {
            const json = await res.json();
            if (json.errors?.[0]?.message) {
              toast.error(json.errors[0].message);
            } else if (json.error) {
              toast.error(json.error);
            } else {
              toast.error(t('error:unPublishingDocument'));
            }
          } catch {
            toast.error(t('error:unPublishingDocument'));
          }
        }
      } catch {
        toast.error(t('error:unPublishingDocument'));
      }
    })().catch(() => {
      toast.error(t('error:unPublishingDocument'));
    });
  }, [dataFromProps, resetForm, collectionSlug, globalSlug, serverURL, api, localeCode, id, i18n.language, incrementVersionCount, setHasPublishedDoc, setMostRecentVersionIsAutosaved, setUnpublishedVersionCount, t]);
  useEffect(() => {
    const hasLocalizedField = traverseForLocalizedFields(entityConfig?.fields);
    setHasLocalizedFields(hasLocalizedField);
  }, [entityConfig?.fields]);
  const drafts = entityConfig?.versions?.drafts;
  const hasDraftsEnabled = typeof drafts === 'object' && drafts !== null;
  const canUnpublish = React.useMemo(() => {
    return hasDraftsEnabled && hasPublishPermission && hasPublishedDoc && !isTrashed;
  }, [hasPublishPermission, hasPublishedDoc, isTrashed, hasDraftsEnabled]);
  const canUnpublishCurrentLocale = React.useMemo(() => {
    if (!canUnpublish || !hasLocalizedFields) {
      return false;
    }
    const isLocalizeStatusEnabled = drafts && drafts.localizeStatus === true;
    const isExperimentalEnabled = config.experimental?.localizeStatus === true;
    return isLocalizeStatusEnabled && isExperimentalEnabled;
  }, [canUnpublish, hasLocalizedFields, drafts, config.experimental?.localizeStatus]);
  const label = getTranslation(localeLabel, i18n);
  if (!canUnpublish) {
    return null;
  }
  return /*#__PURE__*/_jsxs(React.Fragment, {
    children: [/*#__PURE__*/_jsx(PopupList.Button, {
      id: "action-unpublish",
      onClick: () => {
        setUnpublishAll(true);
        toggleModal(unPublishModalSlug);
      },
      children: labelProp || t('version:unpublish')
    }), canUnpublishCurrentLocale && /*#__PURE__*/_jsx(PopupList.Button, {
      id: "action-unpublish-locale",
      onClick: () => {
        setUnpublishAll(false);
        toggleModal(unPublishModalSlug);
      },
      children: labelProp ? `${labelProp} [${label}]` : t('version:unpublishIn', {
        locale: label
      })
    }), /*#__PURE__*/_jsx(ConfirmationModal, {
      body: !unpublishAll ? t('version:aboutToUnpublishIn', {
        locale: label
      }) : t('version:aboutToUnpublish'),
      confirmingLabel: t('version:unpublishing'),
      heading: t('version:confirmUnpublish'),
      modalSlug: unPublishModalSlug,
      onConfirm: () => unpublish(unpublishAll)
    })]
  });
}
//# sourceMappingURL=index.js.map