<?php
namespace NeoDuplicateFull\NeoDuplicate; if (!defined("ABSPATH")) { die("neoWP direct access not allowed"); }





function interface_duplicate_init_20250302() {

    \NeoDuplicateFull\NeoGlobal\enqueue_js("neo-duplicate.js");
}


\NeoDuplicateFull\NeoGlobal\register_rest_endpoint("/wp-json/neo/duplicate", "POST", fn () => \NeoDuplicateFull\NeoGlobal\current_user_can__neo_duplicate(), function ($get_param) {
    $img_url = $get_param("img-url");
    $img_id = attachment_url_to_postid($img_url);
    if ($img_id === 0) { return new \WP_Error("rest_invalid_image", \NeoDuplicateFull\NeoGlobal\neo__("Invalid image URL.", "Ungültige Bild-URL."), ["status" => 404]); }
    $svg_content = \NeoDuplicateFull\NeoGlobal\fs_file_get_contents(get_attached_file($img_id));
    if ($svg_content === false) { return new \WP_Error("rest_invalid_image", \NeoDuplicateFull\NeoGlobal\neo__("Invalid image file.", "Ungültige Bilddatei."), ["status" => 403]); }
    [$new_img_url, $interface_ok] = \NeoDuplicateFull\NeoGlobal\call_interface_func_implemented('\NeoDuplicateFull\NeoDraw\interface_add_svg_to_wp_media_20250526')(
        $svg_content,
        -1,
        dirname(get_attached_file($img_id)),
        basename(get_attached_file($img_id)),
        get_post_field("post_title", $img_id, context: "raw") . \NeoDuplicateFull\NeoGlobal\neo__(" (copy)", " (Kopie)"),
        get_post_field("post_name",  $img_id, context: "raw"),
        \NeoDuplicateFull\NeoGlobal\timestamp_from_wp_date_string(get_the_date("Y-m-d H:i:s", $img_id)),
    );
    if (!$interface_ok) { return new \WP_Error("rest_missing_dependency", \NeoDuplicateFull\NeoGlobal\neo__("neoDraw is not installed", "neoDraw ist nicht installiert"), ["status" => 502]); }
    \NeoDuplicateFull\NeoGlobal\global_log_with_module_name("neo-duplicate", "Duplicate image: " . \NeoDuplicateFull\NeoGlobal\json_encode_better(["imgUrl" => $img_url, "newImgUrl" => $new_img_url]));
    return ["imgUrl" => $new_img_url];
});
