// packages/block-library/src/audio/edit.js
import clsx from "clsx";
import { isBlobURL } from "@wordpress/blob";
import {
  Disabled,
  SelectControl,
  Spinner,
  ToggleControl,
  __experimentalToolsPanel as ToolsPanel,
  __experimentalToolsPanelItem as ToolsPanelItem
} from "@wordpress/components";
import {
  BlockControls,
  BlockIcon,
  InspectorControls,
  MediaPlaceholder,
  MediaReplaceFlow,
  useBlockProps,
  useBlockEditingMode
} from "@wordpress/block-editor";
import { __, _x } from "@wordpress/i18n";
import { useDispatch } from "@wordpress/data";
import { audio as icon } from "@wordpress/icons";
import { store as noticesStore } from "@wordpress/notices";
import { useState } from "@wordpress/element";
import { createUpgradedEmbedBlock } from "../embed/util";
import {
  useUploadMediaFromBlobURL,
  useToolsPanelDropdownMenuProps
} from "../utils/hooks";
import { Caption } from "../utils/caption";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var ALLOWED_MEDIA_TYPES = ["audio"];
function AudioEdit({
  attributes,
  className,
  setAttributes,
  onReplace,
  isSelected: isSingleSelected,
  insertBlocksAfter
}) {
  const { id, autoplay, loop, preload, src } = attributes;
  const [temporaryURL, setTemporaryURL] = useState(attributes.blob);
  const blockEditingMode = useBlockEditingMode();
  const hasNonContentControls = blockEditingMode === "default";
  useUploadMediaFromBlobURL({
    url: temporaryURL,
    allowedTypes: ALLOWED_MEDIA_TYPES,
    onChange: onSelectAudio,
    onError: onUploadError
  });
  function toggleAttribute(attribute) {
    return (newValue) => {
      setAttributes({ [attribute]: newValue });
    };
  }
  function onSelectURL(newSrc) {
    if (newSrc !== src) {
      const embedBlock = createUpgradedEmbedBlock({
        attributes: { url: newSrc }
      });
      if (void 0 !== embedBlock && onReplace) {
        onReplace(embedBlock);
        return;
      }
      setAttributes({ src: newSrc, id: void 0, blob: void 0 });
      setTemporaryURL();
    }
  }
  const { createErrorNotice } = useDispatch(noticesStore);
  function onUploadError(message) {
    createErrorNotice(message, { type: "snackbar" });
  }
  function getAutoplayHelp(checked) {
    return checked ? __("Autoplay may cause usability issues for some users.") : null;
  }
  function onSelectAudio(media) {
    if (!media || !media.url) {
      setAttributes({
        src: void 0,
        id: void 0,
        caption: void 0,
        blob: void 0
      });
      setTemporaryURL();
      return;
    }
    if (isBlobURL(media.url)) {
      setTemporaryURL(media.url);
      return;
    }
    setAttributes({
      blob: void 0,
      src: media.url,
      id: media.id,
      caption: media.caption
    });
    setTemporaryURL();
  }
  const classes = clsx(className, {
    "is-transient": !!temporaryURL
  });
  const blockProps = useBlockProps({
    className: classes
  });
  const dropdownMenuProps = useToolsPanelDropdownMenuProps();
  if (!src && !temporaryURL) {
    return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(
      MediaPlaceholder,
      {
        icon: /* @__PURE__ */ jsx(BlockIcon, { icon }),
        onSelect: onSelectAudio,
        onSelectURL,
        accept: "audio/*",
        allowedTypes: ALLOWED_MEDIA_TYPES,
        value: attributes,
        onError: onUploadError
      }
    ) });
  }
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    isSingleSelected && /* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx(
      MediaReplaceFlow,
      {
        mediaId: id,
        mediaURL: src,
        allowedTypes: ALLOWED_MEDIA_TYPES,
        accept: "audio/*",
        onSelect: onSelectAudio,
        onSelectURL,
        onError: onUploadError,
        onReset: () => onSelectAudio(void 0)
      }
    ) }),
    /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(
      ToolsPanel,
      {
        label: __("Settings"),
        resetAll: () => {
          setAttributes({
            autoplay: false,
            loop: false,
            preload: void 0
          });
        },
        dropdownMenuProps,
        children: [
          /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              label: __("Autoplay"),
              isShownByDefault: true,
              hasValue: () => !!autoplay,
              onDeselect: () => setAttributes({
                autoplay: false
              }),
              children: /* @__PURE__ */ jsx(
                ToggleControl,
                {
                  __nextHasNoMarginBottom: true,
                  label: __("Autoplay"),
                  onChange: toggleAttribute("autoplay"),
                  checked: !!autoplay,
                  help: getAutoplayHelp
                }
              )
            }
          ),
          /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              label: __("Loop"),
              isShownByDefault: true,
              hasValue: () => !!loop,
              onDeselect: () => setAttributes({
                loop: false
              }),
              children: /* @__PURE__ */ jsx(
                ToggleControl,
                {
                  __nextHasNoMarginBottom: true,
                  label: __("Loop"),
                  onChange: toggleAttribute("loop"),
                  checked: !!loop
                }
              )
            }
          ),
          /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              label: __("Preload"),
              isShownByDefault: true,
              hasValue: () => !!preload,
              onDeselect: () => setAttributes({
                preload: void 0
              }),
              children: /* @__PURE__ */ jsx(
                SelectControl,
                {
                  __next40pxDefaultSize: true,
                  __nextHasNoMarginBottom: true,
                  label: _x(
                    "Preload",
                    "noun; Audio block parameter"
                  ),
                  value: preload || "",
                  onChange: (value) => setAttributes({
                    preload: value || void 0
                  }),
                  options: [
                    { value: "", label: __("Browser default") },
                    { value: "auto", label: __("Auto") },
                    { value: "metadata", label: __("Metadata") },
                    {
                      value: "none",
                      label: _x("None", "Preload value")
                    }
                  ]
                }
              )
            }
          )
        ]
      }
    ) }),
    /* @__PURE__ */ jsxs("figure", { ...blockProps, children: [
      /* @__PURE__ */ jsx(Disabled, { isDisabled: !isSingleSelected, children: /* @__PURE__ */ jsx("audio", { controls: "controls", src: src ?? temporaryURL }) }),
      !!temporaryURL && /* @__PURE__ */ jsx(Spinner, {}),
      /* @__PURE__ */ jsx(
        Caption,
        {
          attributes,
          setAttributes,
          isSelected: isSingleSelected,
          insertBlocksAfter,
          label: __("Audio caption text"),
          showToolbarButton: isSingleSelected && hasNonContentControls
        }
      )
    ] })
  ] });
}
var edit_default = AudioEdit;
export {
  edit_default as default
};
//# sourceMappingURL=edit.js.map
